メインコンテンツに移動

メインナビゲーション

  • ホーム
  • サイトマップ
  • ビデオ
  • ご連絡

パンくず

  • ホーム
  • Drupalのモジュール(Rules)でコンテンツ作成時に自動的にSummaryフィールドの値の追加

Drupalのモジュール(Rules)でコンテンツ作成時に自動的にSummaryフィールドの値の追加

drupal
module usage
rules

やりたいこと:コンテンツ(例:記事)作成時にSummaryフィールドに自動的に値の追加

  • なぜSummaryに書き込み:SEO対策として、ページのSummaryをdescriptionメタタグに入れたいです
  • Summaryの構成:記事のBodyにあるh2タグの内容(h2で記事の要点を記述するため)を抽出して、自動にSummaryにセット
  • 作成時のみで自動にセット、記事変更時には自動的にセット/上書きを行わないです(Summaryを手動で変更する可能性があるため)。

モジュール(Rules)で記事新規作成時に自動的にBodyにあるh2タグの内容をSummaryに追加

  • 環境:Drupal7.54、Rules7.x-2.9、PHP Filter7.54(コアーに含まれている)
  • Rules管理画面(Home » Administration » Configuration » Workflow » Rules)で新規ルールの追加
    • イベント: After saving new content、適切なコンテンツ(例:Article)を選択
    • コンディション: Contents is of type、 適切なコンテンツ(例:Article)を選択 【重要】これを選択しないと、次のステップのアクション選択肢が表示されない
    • アクション: Set a data value、 PHPコードを利用して、Bodyのh2タグの内容を抽出し、Summaryにセットします
      <?php
      
        $doc = new DOMDocument();
        libxml_use_internal_errors(true);
        $doc->loadHTML( mb_convert_encoding($node->body['und'][0]['value'], 'HTML-ENTITIES', 'UTF-8') );
        
        $h2 = $doc->getElementsByTagName('h2');
      
        if(count($h2)>0){
          $out = "<ul>";
      	for($c = 0; $c < $h2->length; $c++){ 
            $out .=  "<li>".$h2->item($c)->nodeValue."</li>" ; 
          }
      	$out .= "</ul>";
      	
      	echo $out;
      
        }
         
      ?>
  • 新規記事(Article)を作成して、Bodyにあるh2の内容がSummaryに自動的に設定されたことを確認します
  • 具体的にステップはビデオでご確認ください
Embedded thumbnail for Drupalのモジュール(Rules)でコンテンツ作成時に自動的にSummaryフィールドの値の追加
ホーム

古松

検索

Article Category

  • apache(7)
  • css(19)
  • drupal(295)
  • Electron(4)
  • html(34)
  • javascript(27)
  • laravel(4)
  • linux(5)
  • macOS(2)
  • mysql(13)
  • php(19)
  • python(4)
  • SEO(12)
  • video(72)
  • Visual Studio Code(4)
  • windows(13)
  • wordpress(32)