Silverstripe 3.5.1 - 检查内部链接是否是重定向器页面,如果是,请检查重定向器页面的链接类型

时间:2017-06-19 17:38:09

标签: silverstripe

我正在为网站更新旋转横幅广告功能,该网站允许内部链接与每个横幅图片相关联。有些横幅需要链接到外部网站页面,对于那些,我引用了网络树中的重定向页面,并将重定向设置为另一个网站。"

但是,外部链接无法在新选项卡中打开。我试图找出一种方法来检查内部链接值是否是重定向器页面类型,如果是,如果该重定向器页面设置为外部链接。如果是,则添加" target = _blank。"

我已尝试过但没有运气 - 无论我为重定向器页面设置了哪种类型的链接,链接始终会在同一窗口中打开:

   <div class="Top-Banner clearfix" <% if $BackgroundImage %>style="background: url('$BackgroundImage.URL');"<% end_if %>>
        <% if $InternalURL %>
            <a href="$InternalURL.Link"
            <% if $ClassName = 'RedirectorPage' %>
                <% if $RedirectionType = 'External' %>
               target="_blank"<% end_if %>
            <% end_if %>>
        <% end_if %>
        <div id="Home-Banner-{$Pos}" class="Top-Banner-Inner clearfix" style="background: url('$Image.URL');background-size:cover !important;background-position:center center !important;">
            <div class="Top-Banner-Content clearfix">
                <h1 class="Top-Banner-Headline">$Header</h1>
                <div class="Top-Banner-Sub-Text">
                    $Body
                </div>
                <div class="Top-Banner-Buttons clearfix">
                    <input type="button" value="Patient Portal - Login" onmouseover="this.style.background='#$PatientPortalBtnHoverColor'" onmouseout="this.style.background='#$PatientPortalBtnColor'" style="background-color: #$PatientPortalBtnColor;" onclick="window.location.href='$PatientPortalInternalURL.Link'" />
                    <input type="button" value="Learn More" onmouseover="this.style.background='#$LearnMoreBtnHoverColor'" onmouseout="this.style.background='#$LearnMoreBtnColor'" style="background-color: #$LearnMoreBtnColor;" onclick="window.location.href='$LearnMoreInternalURL.Link'" />
                </div>
            </div>
        </div>
        <% if $InternalURL %>
            </a>
        <% end_if %>
    </div>

也尝试过:

<% if $InternalURL %>
    <a href="$InternalURL.Link"
       <% if $RedirectionType='External' %>
       target="_blank"<% end_if %>>
<% end_if %>

还试过这个:

  <% if $InternalURL %>
    <a href="$InternalURL.Link"
        <% if $InternalURL.ClassName = 'RedirectorPage' %>
           target="_blank"
        <% end_if %>>
    <% end_if %>

,因为它确实将页面类型识别为RedirectorPage,我可以设置target =&#34; _blank&#34;以及其他任何东西,但将它与$ RedirectionType =&#39;外部&#34;结合使用不起作用:

 <% if $InternalURL %>
    <a href="$InternalURL.Link"
        <% if $InternalURL.ClassName = 'RedirectorPage' %>
            <% if $RedirectionType = 'Internal' %>target="_blank"<% end_if %>
        <% end_if %>>
 <% end_if %>

我需要能够检查,但是如果重定向器页面设置为外部链接 - 我不能仅仅因为链接来自重定向器页面而在新窗口中打开链接。内容管理器可能希望使用内部链接,并在单击链接时保持在同一页面上。

以下是RotatorImage数据对象的代码:

<?php

class RotatorImage extends DataObject {

    private static $db = array(
        'SortOrder' => 'Int',
        'Header' => 'varchar',
        'Body' => 'HTMLText',
    );

    // One-to-one relationship with gallery page
    private static $has_one = array(
        'Image' => 'Image',
        'BackgroundImage' => 'Image',
        'Page' => 'Page',
        'InternalURL' => 'SiteTree',
    );

    // tidy up the CMS by not showing these fields
    public function getCMSFields() {
        $fields = parent::getCMSFields();
        $fields->removeFieldFromTab("Root.Main","PageID");
        $fields->removeFieldFromTab("Root.Main","SortOrder");
        $fields->addFieldToTab('Root.Main', new TreeDropdownField("InternalURLID","Banner Link","SiteTree"));

        return $fields;
    }

    // Tell the datagrid what fields to show in the table
    private static $summary_fields = array(
        'ID' => 'ID',
        'Title' => 'Title',
        'Thumbnail' => 'Thumbnail',
        'InternalURLID' => 'Internal URL',
    );

    // this function creates the thumnail for the summary fields to use
    public function getThumbnail() {
        return $this->Image()->CMSThumbnail();
    }

    public function canEdit($member=null) {
        return true;
    }

    public function canDelete($member=null) {
        return true;
    }

    public function canCreate($member=null){
        return true;
    }

    public function canPublish($member=null){
        return true;
    }

    public function canView($member=null){
        return true;
    }
}

2 个答案:

答案 0 :(得分:2)

您是否在Page对象或Banner对象上循环有点不清楚,但如果它是Pages,则以下内容可以正常工作:

    <% loop $Pages %>
        <% if $ClassName = 'RedirectorPage' %>
            <% if $RedirectionType == "External" %>
                <a href="$Link" target="_blank">$Title (External Redirect)</a>
            <% else %>
                <a href="$Link">$Title (Internal Redirect)</a>
            <% end_if %>
        <% else %> 
            <a href="$Link>$Title (Page Link)</a>
        <% end_if %>
    <% end_loop %>

如果它是Banner对象或类似的东西,您需要确保检查Page对象上的RedirectionType,而不是Banner本身:

    <% loop $Banners %>
        <% if $Page.ClassName = 'RedirectorPage' %>
            <% if $Page.RedirectionType == "External" %>
                <a href="$Page.Link" target="_blank">$Page.Title (External Redirect)</a>
            <% else %>
                <a href="$Page.Link">$Page.Title (Internal Redirect)</a>
            <% end_if %>
        <% else %> 
            <a href="$Page.Link>$Page.Title (Page Link)</a>
        <% end_if %>
    <% end_loop %>

如果这还没有确保请发布您的数据对象,我可以再看看。

答案 1 :(得分:0)

我想我在玩了一下之后找到了一个解决方案:

 <% if $InternalURL %>
     <a href="$InternalURL.Link"
          <% if **$InternalURL.RedirectionType == 'External'** %>
               target="_blank"<% end_if %>>
  <% end_if %>

我发现你可以使用上面的方法检查所选页面的重定向类型。在我试验一段时间之前,永远不会猜到它是可能的。