फ़ील्ड फ़ॉर्मैटर बनाना
फील्ड फॉर्मैटर मॉड्यूल फील्ड डेटा को फाइनल यूज़र को दिखाने के लिए फॉर्मैट करता है। फील्ड फॉर्मैटर्स को प्लगइन्स के रूप में परिभाषित किया जाता है, इसलिए नया फील्ड फॉर्मैटर लिखने से पहले प्लगइन API से परिचित होना अनुशंसित है।
फील्ड फॉर्मैटर क्लास
फ़ाइल: /modules/random/src/Plugin/Field/FieldFormatter/RandomDefaultFormatter.php
<?php namespace Drupal\random\Plugin\Field\FieldFormatter; use Drupal\Core\Field\FormatterBase; use Drupal\Core\Field\FieldItemListInterface; /** * 'Random_default' फॉर्मैटर का प्लगइन इम्प्लीमेंटेशन। * * @FieldFormatter( * id = "Random_default", * label = @Translation("Random text"), * field_types = { * "Random" * } * ) */ class RandomDefaultFormatter extends FormatterBase { /** * {@inheritdoc} */ public function settingsSummary() { $summary = []; $summary[] = $this->t('Displays the random string.'); return $summary; } /** * {@inheritdoc} */ public function viewElements(FieldItemListInterface $items, $langcode) { $element = []; foreach ($items as $delta => $item) { // प्रत्येक एलिमेंट को मार्कअप के रूप में रेंडर करें। $element[$delta] = ['#markup' => $item->value]; } return $element; } }
फॉर्मैटर सेटिंग्स
यदि आपके फॉर्मैटर को कस्टम डिस्प्ले सेटिंग्स की आवश्यकता है, तो इसके लिए तीन स्टेप्स करने होते हैं:
- PluginSettingsBase::defaultSettings() को ओवरराइड करके डिफ़ॉल्ट वैल्यू सेट करें
- अपनी सेटिंग्स के लिए कॉन्फ़िगरेशन स्कीमा बनाएँ
- फॉर्म बनाएँ ताकि यूज़र सेटिंग्स बदल सकें
स्टेप 1: PluginSettingsBase::defaultSettings() को ओवरराइड करना
/** * {@inheritdoc} */ public static function defaultSettings() { return [ // 'text_length' नाम की एक सेटिंग घोषित करें, // डिफ़ॉल्ट वैल्यू 'short' होगी 'text_length' => 'short', ] + parent::defaultSettings(); }
स्टेप 2: स्कीमा कॉन्फ़िगरेशन बनाएँ
स्कीमा कॉन्फ़िगरेशन इस फ़ाइल में जाएगी:
[MODULE ROOT]/config/schema/[MODULE_NAME].schema.yml
इस फ़ाइल में आप defaultSettings() में बनाई गई सेटिंग्स का डेटा टाइप बताएँगे।
field.formatter.settings.[FORMATTER ID]: type: mapping label: 'FORMATTER NAME text length' mapping: text_length: type: string label: 'Text Length'
स्टेप 3: यूज़र को सेटिंग्स बदलने देने के लिए फॉर्म बनाएँ
इसके लिए FormatterBase::settingsForm() को ओवरराइड करना होगा।
फ़ाइल की शुरुआत में FormStateInterface
नेमस्पेस जोड़ें:
use Drupal\Core\Form\FormStateInterface;
/** * {@inheritdoc} */ public function settingsForm(array $form, FormStateInterface $form_state) { $form['text_length'] = [ '#title' => $this->t('Text length'), '#type' => 'select', '#options' => [ 'short' => $this->t('Short'), 'long' => $this->t('Long'), ], '#default_value' => $this->getSetting('text_length'), ]; return $form; }
सेटिंग्स फॉर्म्स में #ajax का उपयोग
सेटिंग्स फॉर्म्स में #ajax का उपयोग थोड़ा जटिल है, क्योंकि settingsForm() से बनी फॉर्म पूरी फॉर्म के रूट पर नहीं होती, बल्कि गहराई में नेस्टेड होती है।
उदाहरण: नीचे फॉर्म में दो सेटिंग्स हैं — display_type (label या entity) और entity_display_mode (full या teaser)। entity_display_mode केवल तब दिखेगा जब display_type "entity" होगा।
public function settingsForm(array $form, FormStateInterface $form_state) { $form['display_type'] = [ '#title' => $this->t('Display Type'), '#type' => 'select', '#options' => [ 'label' => $this->t('Label'), 'entity' => $this->t('Entity'), ], '#default_value' => $this->getSetting('display_type'), '#ajax' => [ 'wrapper' => 'private_message_thread_member_formatter_settings_wrapper', 'callback' => [$this, 'ajaxCallback'], ], ]; $form['entity_display_mode'] = [ '#prefix' => '<div id="private_message_thread_member_formatter_settings_wrapper">', '#suffix' => '</div>', ]; $field_name = $this->fieldDefinition->getItemDefinition()->getFieldDefinition()->getName(); $setting_key = 'display_type'; if($value = $form_state->getValue(['fields', $field_name, 'settings_edit_form', 'settings', $setting_key])) { $display_type = $value; } else { $display_type = $this->getSetting('display_type'); } if($display_type == 'entity') { $form['entity_display_mode']['#type'] = 'select'; $form['entity_display_mode']['#title'] = $this->t('View mode'); $form['entity_display_mode']['#options'] = [ 'full' => $this->t('Full'), 'teaser' => $this->t('Teaser'), ]; $form['entity_display_mode']['#default_value'] = $this->getSetting('entity_display_mode'); } else { $form['entity_display_mode']['#markup'] = ''; } return $form; }
फिर AJAX callback बनाएँ और उपयुक्त फॉर्म एलिमेंट रिटर्न करें:
public function ajaxCallback(array $form, FormStateInterface $form_state) { $field_name = $this->fieldDefinition->getItemDefinition()->getFieldDefinition()->getName(); $element_to_return = 'entity_display_mode'; return $form['fields'][$field_name]['plugin']['settings_edit_form']['settings'][$element_to_return]; }
फील्ड फॉर्मैटर्स में डिपेंडेंसी इंजेक्शन
फील्ड फॉर्मैटर्स में डिपेंडेंसी इंजेक्शन का उपयोग करने के लिए तीन स्टेप्स करने होते हैं:
- ContainerFactoryPluginInterface को इम्प्लीमेंट करें
- ContainerFactoryPluginInterface::create() को इम्प्लीमेंट करें
- FormatterBase::__construct() को ओवरराइड करें
1) ContainerFactoryPluginInterface इम्प्लीमेंट करना
use Drupal\Core\Plugin\ContainerFactoryPluginInterface; class MyFormatter extends FormatterBase implements ContainerFactoryPluginInterface {
2) ContainerFactoryPluginInterface::create() इम्प्लीमेंट करना
उदाहरण: entity.manager सर्विस को फॉर्मैटर में इंजेक्ट करना
use Symfony\Component\DependencyInjection\ContainerInterface; public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { return new static( $plugin_id, $plugin_definition, $configuration['field_definition'], $configuration['settings'], $configuration['label'], $configuration['view_mode'], $configuration['third_party_settings'], // यहां अपनी सर्विस इंजेक्ट करें $container->get('entity.manager') ); }
3) FormatterBase::__construct() को ओवरराइड करना
__construct() को ओवरराइड करें, parent::__construct() को कॉल करें और फिर सर्विस को क्लास प्रॉपर्टी में सेव करें।
use Drupal\Core\Field\FieldDefinitionInterface; /** * entity manager सर्विस * * @var \Drupal\Core\Entity\EntityManagerInterface */ protected $entityManager; /** * MyFormatter ऑब्जेक्ट कन्स्ट्रक्टर। */ public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, $label, $view_mode, array $third_party_settings, EntityManagerInterface $entityManager) { parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $label, $view_mode, $third_party_settings); $this->entityManager = $entityManager; }
अब आप अपने फॉर्मैटर क्लास में कहीं भी $this->entityManager
का उपयोग कर सकते हैं।