logo

Լրացուցիչ Բլոկների Տեսակներ (EBT) - Դասավորության Կառուցողի նոր փորձառություն❗

Լրացուցիչ Բլոկների Տեսակներ (EBT) - ձևավորված, կարգավորելի բլոկների տեսակներ՝ սլայդշոուներ, ներդիրներ, քարտեր, բացվող ցանկեր և շատ ուրիշներ։ Ներառված կարգավորումներ՝ ֆոնի, DOM տուփի, JavaScript փլագինների համար։ Փորձեք դասավորությունների կառուցման ապագան արդեն այսօր։

EBT մոդուլների ցուցադրական տարբերակներ Ներբեռնել EBT մոդուլները

❗Լրացուցիչ Պարբերությունների Տեսակներ (EPT) - Պարբերությունների նոր փորձառություն

Լրացուցիչ պարբերության տեսակներ (EPT) - անալոգիական պարբերության վրա հիմնված մոդուլների հավաքակազմ։

EPT մոդուլների ցուցադրական տարբերակներ Ներբեռնել EPT մոդուլները

Scroll
20/06/2025, by Ivan

Menu

Դաշտի ֆորմատավորման մոդուլը ֆորմատավորում է դաշտի տվյալները, որպեսզի դրանք դիտարկվեն վերջնական օգտագործողի կողմից։ Դաշտի ֆորմատավորողներն սահմանվում են որպես plugin-ներ, դրա համար խորհուրդ է տրվում ծանոթանալ plugin 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' ֆորմատավորողի plugin-ի իրականացում։
 *
 * @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) {
      // Յուրաքանչյուր էլեմենտի արտածում որպես markup։
      $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()-ում։

Քայլ 1-ում ստեղծվեց 'text_length' անունով կարգավորում, որի արժեքը տեքստային է։ Սքիման կդրսևորվի այսպես․

field.formatter.settings.[FORMATTER ID]:
  type: mapping
  label: 'FORMATTER NAME տեքստի երկարություն'
  mapping:
    text_length:
      type: string
      label: 'Տեքստի երկարություն'

Քայլ 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 (entity-ի ցուցադրման ռեժիմ՝ full կամ teaser)։ Entity-ի ցուցադրման ռեժիմը ցուցադրվում է միայն 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' => '
', '#suffix' => '
', ]; // Ստանալ տվյալ դաշտի անունը $field_name = $this->fieldDefinition->getItemDefinition()->getFieldDefinition()->getName(); // Սահմանել կարգավորման բանալի $setting_key = 'display_type'; // Փորձել ստանալ արժեք form_state-ից, եթե չկա՝ օգտագործել լռելյայնը 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 { // Ելքագրման ապահովում, որպեսզի AJAX wrapper-ը ցուցադրվի $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];
}

Ծառայությունների ներարկում դաշտի ֆորմատորներում

Ծառայությունների ներարկումը դաշտի ֆորմատորներում պահանջում է երեք քայլ՝

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() կոնստրուկտորը FormatterBase-ում, միաժամանակ կանչելով parent::__construct(), ապա պահպանեք ծառայությունը դասի հատկությունում՝

use Drupal\Core\Field\FieldDefinitionInterface;

/**
 * Սուբյեկտի մենեջեր ծառայությունը
 *
 * @var \Drupal\Core\Entity\EntityManagerInterface
 */
protected $entityManager;

/**
 * MyFormatter օբյեկտի կոնստրուկտոր։
 *
 * @param string $plugin_id
 *   Plugin-ի ID-ն։
 * @param mixed $plugin_definition
 *   Plugin-ի սահմանման տվյալները։
 * @param \Drupal\Core\Field\FieldDefinitionInterface $field_definition
 *   Դաշտի սահմանման ինտերֆեյս։
 * @param array $settings
 *   Ֆորմատավորման կարգավորումները։
 * @param string $label
 *   Ֆորմատավորման լեյբլի կարգավորումը։
 * @param string $view_mode
 *   Դիտման ռեժիմը։
 * @param array $third_party_settings
 *   Երրորդ կողմի կարգավորումները։
 * @param \Drupal\Core\Entity\EntityManagerInterface $entityManager
 *   Սուբյեկտի մենեջեր ծառայությունը։
 */
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 սուբյեկտի մենեջերը։

Drupal’s online documentation is © 2000-2020 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution-ShareAlike 2.0. PHP code is distributed under the GNU General Public License.