搜尋
首頁後端開發php教程Drupal 8中的自定義顯示套件字段

顯示套件:掌握Drupal 8

>中的自定義字段創建

> Display Suite(DS)仍然是Drupal貢獻模塊的基石,為製作網站佈局提供了強大的工具並管理內容演示文稿。 它的強度在於創建與DS佈局中核心字段值一起顯示的自定義字段。 該功能在Drupal 7中高度重視,在Drupal 8中繼續和擴展,利用了新的面向對象的編程(OOP)體系結構和插件系統。本指南詳細介紹了Drupal 8中創建自定義DS字段

Custom Display Suite Fields in Drupal 8

> drupal 8插件和DS字段插件類型>

> Drupal 8的插件系統取代了Drupal 7的掛鉤。 DS利用此系統,揭露

>插件類型。 我們不是_info,而是在其方法的註釋和邏輯中構建一個帶有元數據的插件類。 DsField> hook_ds_field_info()

創建

插件類VocabularyTerms> >我們的示例創建了一個DS字段(在名為“演示”的自定義模塊中),顯示了可配置詞彙的分類術語,僅限於文章節點。 插件類(

)位於

>中,並註釋如下:> 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 {
}

為了允許詞彙選擇,我們實現了設置默認詞彙(“ tags”):

defaultConfiguration()formatters(例如,鏈接或未鏈接的術語列表)是使用

>方法定義的:
/**
 * {@inheritdoc}
 */
public function defaultConfiguration() {
  return ['vocabulary' => 'tags'];
}
>

formatters()

/**
 * {@inheritdoc}
 */
public function formatters() {
  return ['linked' => 'Linked', 'unlinked' => 'Unlinked'];
}

Custom Display Suite Fields in Drupal 8 >配置摘要和設置表單

> 方法提供了所選配置的UI摘要:>

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.'];
}

方法創建用於詞彙選擇的UI:> Custom Display Suite Fields in Drupal 8

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;
}
渲染字段

Custom Display Suite Fields in Drupal 8

方法查詢並呈現術語:> 輔助方法(

)根據所選格式處理術語格式。 記住要注入服務,而不是在生產環境中使用靜態調用。 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),
  ];
}

buildTermList()buildTermListItem()結論

該綜合指南演示了在Drupal 8中創建自定義DS字段,並展示了插件系統的功能和靈活性。 請記住在實施代碼後清除緩存。 這種增強的方法為擴展顯示套件功能提供了一種可靠且可維護的方法。 >

以上是Drupal 8中的自定義顯示套件字段的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
高流量網站的PHP性能調整高流量網站的PHP性能調整May 14, 2025 am 12:13 AM

TheSecretTokeEpingAphp-PowerEdwebSiterUnningSmoothlyShyunderHeavyLoadInVolvOLVOLVOLDEVERSALKEYSTRATICES:1)emplactopCodeCachingWithOpcachingWithOpCacheToreCescriptexecution Time,2)使用atabasequercachingCachingCachingWithRedataBasEndataBaseLeSendataBaseLoad,3)

PHP中的依賴注入:初學者的代碼示例PHP中的依賴注入:初學者的代碼示例May 14, 2025 am 12:08 AM

你應該關心DependencyInjection(DI),因為它能讓你的代碼更清晰、更易維護。 1)DI通過解耦類,使其更模塊化,2)提高了測試的便捷性和代碼的靈活性,3)使用DI容器可以管理複雜的依賴關係,但要注意性能影響和循環依賴問題,4)最佳實踐是依賴於抽象接口,實現鬆散耦合。

PHP性能:是否可以優化應用程序?PHP性能:是否可以優化應用程序?May 14, 2025 am 12:04 AM

是的,優化papplicationispossibleandessential.1)empartcachingingcachingusedapcutorediucedsatabaseload.2)優化的atabaseswithexing,高效Quereteries,and ConconnectionPooling.3)EnhanceCodeWithBuilt-unctions,避免使用,避免使用ingglobalalairaiables,並避免使用

PHP性能優化:最終指南PHP性能優化:最終指南May 14, 2025 am 12:02 AM

theKeyStrategiestosigantificallyBoostPhpaPplicationPerformenCeare:1)UseOpCodeCachingLikeLikeLikeLikeLikeCacheToreDuceExecutiontime,2)優化AtabaseInteractionswithPreparedStateTementStatementStatementAndProperIndexing,3)配置

PHP依賴注入容器:快速啟動PHP依賴注入容器:快速啟動May 13, 2025 am 12:11 AM

aphpdepentioncontiveContainerIsatoolThatManagesClassDeptions,增強codemodocultion,可驗證性和Maintainability.itactsasaceCentralHubForeatingingIndections,因此reducingTightCightTightCoupOulplingIndeSingantInting。

PHP中的依賴注入與服務定位器PHP中的依賴注入與服務定位器May 13, 2025 am 12:10 AM

選擇DependencyInjection(DI)用於大型應用,ServiceLocator適合小型項目或原型。 1)DI通過構造函數注入依賴,提高代碼的測試性和模塊化。 2)ServiceLocator通過中心註冊獲取服務,方便但可能導致代碼耦合度增加。

PHP性能優化策略。PHP性能優化策略。May 13, 2025 am 12:06 AM

phpapplicationscanbeoptimizedForsPeedAndeffificeby:1)啟用cacheInphp.ini,2)使用preparedStatatementSwithPdoforDatabasequesies,3)3)替換loopswitharray_filtaray_filteraray_maparray_mapfordataprocrocessing,4)conformentnginxasaseproxy,5)

PHP電子郵件驗證:確保正確發送電子郵件PHP電子郵件驗證:確保正確發送電子郵件May 13, 2025 am 12:06 AM

phpemailvalidation invoLvesthreesteps:1)格式化進行regulareXpressecthemailFormat; 2)dnsvalidationtoshethedomainhasavalidmxrecord; 3)

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

將Eclipse與SAP NetWeaver應用伺服器整合。

SublimeText3 英文版

SublimeText3 英文版

推薦:為Win版本,支援程式碼提示!

SecLists

SecLists

SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser是一個安全的瀏覽器環境,安全地進行線上考試。該軟體將任何電腦變成一個安全的工作站。它控制對任何實用工具的訪問,並防止學生使用未經授權的資源。