ディスプレイスイート:DRUPAL 8
でカスタムフィールド作成をマスターするディスプレイスイート(DS)は、Drupalの寄稿されたモジュールの基礎であり、サイトレイアウトを作成し、コンテンツプレゼンテーションを管理するための堅牢なツールを提供しています。 その強みは、DSレイアウト内のコアフィールド値とともに表示されるカスタムフィールドを作成することにあります。 Drupal 7で高く評価されているこの機能は、Drupal 8で継続および拡大し、新しいオブジェクト指向プログラミング(OOP)アーキテクチャおよびプラグインシステムを活用しています。このガイドの詳細Drupal 8のカスタムDSフィールドの作成
DRUPAL 8プラグインとDSフィールドプラグインタイプ
Drupal 8のプラグインシステムは、Drupal 7のフックを置き換えます。 DSはこのシステムを利用して、プラグインタイプを公開します。の代わりに、メタデータがその注釈とそのメソッド内でロジックを含むプラグインクラスを構築します。
_info
DsField
hook_ds_field_info()
プラグインクラス
の作成
私たちの例は、記事ノードに限定された構成可能な語彙から分類用語を表示するDSフィールド(「デモ」という名前のカスタムモジュール内)を作成します。 プラグインのクラス(VocabularyTerms
)はに存在し、次のように注釈が付けられています。
VocabularyTerms
デフォルトの構成とフォーマッツsrc/plugins/DsField
namespace Drupal\demo\Plugin\DsField; use Drupal\ds\Plugin\DsField\DsFieldBase; /** * Plugin displaying terms from a selected taxonomy vocabulary. * * @DsField( * id = "vocabulary_terms", * title = @Translation("Vocabulary Terms"), * entity_type = "node", * provider = "demo", * ui_limit = {"article|*"} * ) */ class VocabularyTerms extends DsFieldBase { }
にデフォルトの語彙( "タグ")を設定するように実装してください:
フォーマッタ(例:リンクまたはリンクされていないタームリスト)は、メソッドを使用して定義されます。
defaultConfiguration()
/** * {@inheritdoc} */ public function defaultConfiguration() { return ['vocabulary' => 'tags']; }
formatters()
/** * {@inheritdoc} */ public function formatters() { return ['linked' => 'Linked', 'unlinked' => 'Unlinked']; }
settingsSummary()
/** * {@inheritdoc} */ public function settingsSummary($settings) { $config = $this->getConfiguration(); return isset($config['vocabulary']) && $config['vocabulary'] ? ['Vocabulary: ' . \Drupal::entityTypeManager()->getStorage('taxonomy_vocabulary')->load($config['vocabulary'])->label()] : ['No vocabulary selected.']; }
settingsForm()
/** * {@inheritdoc} */ public function settingsForm($form, FormStateInterface $form_state) { $config = $this->getConfiguration(); $vocabularies = \Drupal::entityTypeManager()->getStorage('taxonomy_vocabulary')->loadMultiple(); $options = []; foreach ($vocabularies as $vocabulary) { $options[$vocabulary->id()] = $vocabulary->label(); } $form['vocabulary'] = [ '#type' => 'select', '#title' => $this->t('Vocabulary'), '#default_value' => $config['vocabulary'], '#options' => $options, ]; return $form; }
メソッドは、次の用語をクエリしてレンダリングします
選択したフォーマッタに基づいて、
および)を処理します。 生産環境で静的コールを使用する代わりに、サービスを注入することを忘れないでください。
build()
/** * {@inheritdoc} */ public function build() { $config = $this->getConfiguration(); if (!isset($config['vocabulary']) || !$config['vocabulary']) { return []; } $query = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->getQuery(); $query->condition('vid', $config['vocabulary']); $tids = $query->execute(); $terms = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->loadMultiple($tids); return [ '#theme' => 'item_list', '#items' => $this->buildTermList($terms), ]; }
この包括的なガイドは、プラグインシステムのパワーと柔軟性を紹介するDrupal 8にカスタムDSフィールドを作成することを示しています。 コードを実装した後、キャッシュをクリアすることを忘れないでください。 この強化されたアプローチは、ディスプレイスイート機能を拡張するための堅牢で保守可能な方法を提供します。
以上がDrupal 8のカスタムディスプレイスイートフィールドの詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。