Drupal मॉड्यूल में फ़ील्ड टाइप बनाना
फील्ड टाइप्स फील्ड्स की प्रॉपर्टीज़ और बिहेवियर को परिभाषित करते हैं। फील्ड टाइप्स को प्लगइन्स के रूप में परिभाषित किया जाता है, इसलिए नया फील्ड टाइप लिखने से पहले प्लगइन API से परिचित होना अनुशंसित है।
Drupal 8 में फील्ड टाइप बनाने के लिए, आपको FieldType
एनोटेशन के साथ एक क्लास की आवश्यकता होगी।
स्थान (Location): फील्ड टाइप क्लास को इस स्थान पर होना चाहिए:
MODULE_NAME/src/Plugin/Field/FieldType
उदाहरण: /modules/foo/src/Plugin/Field/FieldType/BazItem.php
नेमस्पेस: क्लास का नेमस्पेस होना चाहिए:
Drupal\MODULE_NAME\Plugin\Field\FieldType
<?php namespace Drupal\MODULE_NAME\Plugin\Field\FieldType;
क्लास के ऊपर लिखे गए डॉकब्लॉक (annotation) में यूनिक आईडी, लेबल और डिफ़ॉल्ट फॉर्मैटर व विजेट की जानकारी होनी चाहिए।
/** * Provides a field type of baz. * * @FieldType( * id = "baz", * label = @Translation("Baz field"), * default_formatter = "baz_formatter", * default_widget = "baz_widget", * ) */
क्लास को FieldItemInterface
इम्प्लीमेंट करना चाहिए और FieldItemBase
को एक्सटेंड करना चाहिए।
class BazItem extends FieldItemBase { }
FieldItemInterface::schema() को ओवरराइड करना अनिवार्य है ताकि सिस्टम को बताया जा सके कि फील्ड के वैल्यूज़ को कैसे स्टोर किया जाए:
/** * {@inheritdoc} */ public static function schema(FieldStorageDefinitionInterface $field_definition) { return array( 'columns' => array( 'value' => array( 'type' => 'text', 'size' => 'tiny', 'not null' => FALSE, ), ), ); }
यह मेथड स्कीमा API कॉलम स्पेसिफिकेशन वाला ऐरे रिटर्न करता है।
FieldItemInterface::propertyDefinitions() मेथड फील्ड प्रॉपर्टीज़ के बारे में जानकारी देता है:
/** * {@inheritdoc} */ public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) { $properties = []; $properties['value'] = DataDefinition::create('string'); return $properties; }
Map::isEmpty() को ओवरराइड कर बताना होगा कि फील्ड खाली कब माना जाएगा:
/** * {@inheritdoc} */ public function isEmpty() { $value = $this->get('value')->getValue(); return $value === NULL || $value === ''; }
फील्ड सेटिंग्स
फील्ड सेटिंग्स उपयोगकर्ताओं को अपनी आवश्यकताओं के अनुसार फील्ड को कस्टमाइज़ करने देती हैं। अगर आपके फील्ड की सेटिंग्स हैं, तो आपको 3 स्टेप्स करने होंगे:
- FieldItemBase::defaultFieldSettings() ओवरराइड करके डिफ़ॉल्ट वैल्यू सेट करें
- उन सेटिंग्स के लिए कॉन्फ़िगरेशन स्कीमा बनाएँ
- यूज़र इंटरफ़ेस के लिए सेटिंग्स फॉर्म बनाएँ
स्टेप 1: defaultFieldSettings() ओवरराइड करें
/** * {@inheritdoc} */ public static function defaultFieldSettings() { return [ 'size' => 'large', ] + parent::defaultFieldSettings(); }
स्टेप 2: कॉन्फ़िगरेशन स्कीमा बनाएँ
[MODULE ROOT]/config/schema/[MODULE_NAME].schema.yml
field.field_settings.[FIELD ID]: type: mapping label: 'FIELDNAME settings' mapping: size: type: string label: 'Size'
स्टेप 3: फील्ड सेटिंग्स फॉर्म बनाएँ
/** * {@inheritdoc} */ public function fieldSettingsForm(array $form, FormStateInterface $form_state) { $element = []; $element['size'] = [ '#title' => $this->t('Size'), '#type' => 'select', '#options' => [ 'small' => $this->t('Small'), 'medium' => $this->t('Medium'), 'large' => $this->t('Large'), ], '#default_value' => $this->getSetting('size'), ]; return $element; }
वास्तविक उदाहरण
RgbItem, Examples प्रोजेक्ट के field_example मॉड्यूल से:
namespace Drupal\field_example\Plugin\Field\FieldType; use Drupal\Core\Field\FieldItemBase; use Drupal\Core\Field\FieldDefinitionInterface; use Drupal\Core\Field\FieldStorageDefinitionInterface; use Drupal\Core\TypedData\DataDefinition; /** * 'field_example_rgb' फील्ड टाइप का प्लगइन इम्प्लीमेंटेशन। * * @FieldType( * id = "field_example_rgb", * label = @Translation("Example Color RGB"), * module = "field_example", * description = @Translation("Demonstrates a field composed of an RGB color."), * default_widget = "field_example_text", * default_formatter = "field_example_simple_text" * ) */ class RgbItem extends FieldItemBase { /** * {@inheritdoc} */ public static function schema(FieldStorageDefinitionInterface $field_definition) { return array( 'columns' => array( 'value' => array( 'type' => 'text', 'size' => 'tiny', 'not null' => FALSE, ), ), ); } /** * {@inheritdoc} */ public function isEmpty() { $value = $this->get('value')->getValue(); return $value === NULL || $value === ''; } /** * {@inheritdoc} */ public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) { $properties['value'] = DataDefinition::create('string') ->setLabel(t('Hex value')); return $properties; } }