LoginSignup
0
0

More than 5 years have passed since last update.

[baserCMS]オプショナルリンク利用時、リンク中にブロック要素を含む場合のサンプル

Last updated at Posted at 2016-07-17

Blogヘルパの getPostLinkUrl() では、記事のURL変更が効かないため、ビュー側で aタグ内の文字列を調整し、postTitle()(or getPostTitle())がそのまま利用できるカタチにします。

想定している対象者

  • baserCMSでウェブサイトを制作をしている方
  • baserCMSでプラグインを制作している方

発生状況

  1. getPostLinkUrl() でブログ記事のURLを表示している際
  2. オプショナルリンクプラグインの機能で、URL書換え処理を指定しても、URLが変更されない

環境

  • baserCMS 3.0.10

原因

getPostLinkUrl() では、URL書換えに必要な beforeGetLink、afterGetLink イベントが走らないため。

対処例

ビュー側で aタグ内の文字列を調整し、postTitle()(or getPostTitle())がそのまま利用できるカタチにします。

<ul>
<?php if ($posts): ?>
    <?php foreach ($posts as $key => $post): ?>
        <?php $links = ''; ?>
        <li>
        <?php
            $links .= '<div class="img-eyecatch">'. $this->Blog->getEyeCatch($post, array('link' => false)) .'</div>';
            $links .= '<div class="txt-date">'. $this->Blog->getPostDate($post, 'Y.m.d') .'</div>';
            $links .= '<div class="txt-title">'. $this->Blog->getPostTitle($post, false) .'</div>';
            $post['BlogPost']['name'] = $links;
        ?>
        <?php $this->Blog->postTitle($post) ?>
        </li>
    <?php endforeach; ?>
<?php else: ?>
    <li class="no-data">記事がありません</li>
<?php endif ?>
</ul>

記事概要を含む場合、記事タイトル箇所のみ特定のタグで囲う場合

  • 記事概要を含む場合は、trim() で前後の余分な空白を削除する
  • 記事タイトル箇所のみ特定のタグで囲う場合は、一時的に実体参照変換を掛けて全て文字列扱いとし、その後、表示の際に decode して戻して必要なhtmlタグを反映させる
    • 記事に「URL: リンクなし」指定時に正しく動作しないため
<?php if ($posts): ?>
    <?php foreach ($posts as $key => $post): ?>
        <dl>
            <dt><?php echo $this->Blog->getPostDate($post, 'Y.m.d') ?>
                <?php if (Hash::get($post, 'BlogCategory.id')): ?>
                    <span class="icon icon_<?php echo Hash::get($post, 'BlogCategory.name') ?>">
                        <?php echo Hash::get($post, 'BlogCategory.title') ?>
                    </span>
                <?php endif ?>
            </dt>
            <dd>
                <?php
                $links = '';
                $links .= '<string>' . $this->Blog->getPostTitle($post, false) . '</string>';
                $links = htmlspecialchars($links);
                $links .= trim(strip_tags(Hash::get($post, 'BlogPost.content')));
                $post['BlogPost']['name'] = $links;
                ?>
                <?php echo htmlspecialchars_decode($this->Blog->getPostTitle($post)) ?>
            </dd>
        </dl>
    <?php endforeach; ?>
<?php else: ?>
    <dl>
        <dt class="no-data">記事がありません</dt>
    </dl>
<?php endif ?>
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0