formElement example


class MediaLibraryInceptionWidget extends MediaLibraryWidget {

  /** * {@inheritdoc} */
  public function formElement(FieldItemListInterface $items$delta, array $element, array &$form, FormStateInterface $form_state) {
    if (empty($element['#element_validate'])) {
      $element['#element_validate'] = [];
    }
    $element['#element_validate'][] = [$this, 'elementValidate'];
    return parent::formElement($items$delta$element$form$form_state);
  }

  /** * {@inheritdoc} */
  public function elementValidate($element, FormStateInterface $form_state$form) {
    $field_name = $element['#field_name'];
    $entity = $form_state->getFormObject()->getEntity();
    $input = $form_state->getUserInput();
    if (!empty($input['_triggering_element_name']) && str_contains($input['_triggering_element_name'], 'media-library-update')) {
      // This will validate a required field before an upload is completed.

  public function settingsForm(array $form, FormStateInterface $form_state) {
    $element = parent::settingsForm($form$form_state);
    $element['rows']['#description'] = $this->t('Text editors may override this setting.');
    return $element;
  }

  /** * {@inheritdoc} */
  public function formElement(FieldItemListInterface $items$delta, array $element, array &$form, FormStateInterface $form_state) {
    $main_widget = parent::formElement($items$delta$element$form$form_state);
    $allowed_formats = $this->getFieldSetting('allowed_formats');

    $element = $main_widget['value'];
    $element['#type'] = 'text_format';
    $element['#format'] = $items[$delta]->format;
    $element['#base_type'] = $main_widget['value']['#type'];

    if ($allowed_formats && !$this->isDefaultValueWidget($form_state)) {
      $element['#allowed_formats'] = $allowed_formats;
    }

    

class EntityReferenceAutocompleteTagsWidget extends EntityReferenceAutocompleteWidget {

  /** * {@inheritdoc} */
  public function formElement(FieldItemListInterface $items$delta, array $element, array &$form, FormStateInterface $form_state) {
    $element = parent::formElement($items$delta$element$form$form_state);

    $element['target_id']['#tags'] = TRUE;
    $element['target_id']['#default_value'] = $items->referencedEntities();

    return $element;
  }

  /** * {@inheritdoc} */
  public function massageFormValues(array $values, array $form, FormStateInterface $form_state) {
    

  protected function formSingleElement(FieldItemListInterface $items$delta, array $element, array &$form, FormStateInterface $form_state) {
    $element += [
      '#field_parents' => $form['#parents'],
      // Only the first widget should be required.       '#required' => $delta == 0 && $this->fieldDefinition->isRequired(),
      '#delta' => $delta,
      '#weight' => $delta,
    ];

    $element = $this->formElement($items$delta$element$form$form_state);

    if ($element) {
      // Allow modules to alter the field widget form element.       $context = [
        'form' => $form,
        'widget' => $this,
        'items' => $items,
        'delta' => $delta,
        'default' => $this->isDefaultValueWidget($form_state),
      ];
      \Drupal::moduleHandler()->alter(['field_widget_single_element_form', 'field_widget_single_element_' . $this->getPluginId() . '_form']$element$form_state$context);
    }

class OEmbedWidget extends StringTextfieldWidget {

  /** * {@inheritdoc} */
  public function formElement(FieldItemListInterface $items$delta, array $element, array &$form, FormStateInterface $form_state) {
    $element = parent::formElement($items$delta$element$form$form_state);

    /** @var \Drupal\media\Plugin\media\Source\OEmbedInterface $source */
    $source = $items->getEntity()->getSource();
    $message = $this->t('You can link to media from the following services: @providers', ['@providers' => implode(', ', $source->getProviders())]);

    if (!empty($element['value']['#description'])) {
      $element['value']['#description'] = [
        '#theme' => 'item_list',
        '#items' => [$element['value']['#description']$message],
      ];
    }
    
if ($this->getSetting('show_summary')) {
      $summary[] = $this->t('Summary field will always be visible');
    }

    return $summary;
  }

  /** * {@inheritdoc} */
  public function formElement(FieldItemListInterface $items$delta, array $element, array &$form, FormStateInterface $form_state) {
    $element = parent::formElement($items$delta$element$form$form_state);

    $display_summary = $items[$delta]->summary || $this->getFieldSetting('display_summary');
    $required = empty($form['#type']) && $this->getFieldSetting('required_summary');

    $element['summary'] = [
      '#type' => $display_summary ? 'textarea' : 'value',
      '#default_value' => $items[$delta]->summary,
      '#title' => $this->t('Summary'),
      '#rows' => $this->getSetting('summary_rows'),
      '#description' => !$required ? $this->t('Leave blank to use trimmed value of full text as the summary.') : '',
      '#attributes' => ['class' => ['text-summary']],
      

class OptionsButtonsWidget extends OptionsWidgetBase {

  /** * {@inheritdoc} */
  public function formElement(FieldItemListInterface $items$delta, array $element, array &$form, FormStateInterface $form_state) {
    $element = parent::formElement($items$delta$element$form$form_state);

    $options = $this->getOptions($items->getEntity());
    $selected = $this->getSelectedOptions($items);

    // If required and there is one single option, preselect it.     if ($this->required && count($options) == 1) {
      reset($options);
      $selected = [key($options)];
    }

    if ($this->multiple) {
      

  protected function buildWidgetForm($entity) {
    $form = [
      '#parents' => [],
    ];
    return $this->container->get('plugin.manager.field.widget')->createInstance('media_library_widget', [
      'field_definition' => $this->baseField,
      'settings' => [],
      'third_party_settings' => [],
    ])->formElement($entity->media, 0, ['#description' => '']$formnew FormState());
  }

}
$configuration['field_definition'],
      $configuration['settings'],
      $configuration['third_party_settings'],
      $container->get('entity_type.manager')->getStorage('date_format')
    );
  }

  /** * {@inheritdoc} */
  public function formElement(FieldItemListInterface $items$delta, array $element, array &$form, FormStateInterface $form_state) {
    $element = parent::formElement($items$delta$element$form$form_state);

    // If the field is date-only, make sure the title is displayed. Otherwise,     // wrap everything in a fieldset, and the title will be shown in the legend.     if ($this->getFieldSetting('datetime_type') === DateTimeItem::DATETIME_TYPE_DATE) {
      $element['value']['#title'] = $this->fieldDefinition->getLabel();
      $element['value']['#description'] = $this->fieldDefinition->getDescription();
    }
    else {
      $element['#theme_wrappers'][] = 'fieldset';
    }

    
else {
      $elements['#file_upload_description'] = $file_upload_help;
    }

    return $elements;
  }

  /** * {@inheritdoc} */
  public function formElement(FieldItemListInterface $items$delta, array $element, array &$form, FormStateInterface $form_state) {
    $element = parent::formElement($items$delta$element$form$form_state);

    $field_settings = $this->getFieldSettings();

    // Add image validation.     $element['#upload_validators']['file_validate_is_image'] = [];

    // Add upload resolution validation.     if ($field_settings['max_resolution'] || $field_settings['min_resolution']) {
      $element['#upload_validators']['file_validate_image_resolution'] = [$field_settings['max_resolution']$field_settings['min_resolution']];
    }

    
return [
      'increment' => '15',
      'date_order' => 'YMD',
      'time_type' => '24',
    ] + parent::defaultSettings();
  }

  /** * {@inheritdoc} */
  public function formElement(FieldItemListInterface $items$delta, array $element, array &$form, FormStateInterface $form_state) {
    $element = parent::formElement($items$delta$element$form$form_state);

    $date_order = $this->getSetting('date_order');

    if ($this->getFieldSetting('datetime_type') == DateRangeItem::DATETIME_TYPE_DATETIME) {
      $time_type = $this->getSetting('time_type');
      $increment = $this->getSetting('increment');
    }
    else {
      $time_type = '';
      $increment = '';
    }

    
$configuration['field_definition'],
      $configuration['settings'],
      $configuration['third_party_settings'],
      $container->get('entity_type.manager')->getStorage('date_format')
    );
  }

  /** * {@inheritdoc} */
  public function formElement(FieldItemListInterface $items$delta, array $element, array &$form, FormStateInterface $form_state) {
    $element = parent::formElement($items$delta$element$form$form_state);

    // Identify the type of date and time elements to use.     switch ($this->getFieldSetting('datetime_type')) {
      case DateRangeItem::DATETIME_TYPE_DATE:
      case DateRangeItem::DATETIME_TYPE_ALLDAY:
        $date_type = 'date';
        $time_type = 'none';
        $date_format = $this->dateStorage->load('html_date')->getPattern();
        $time_format = '';
        break;

      
return [
      'increment' => '15',
      'date_order' => 'YMD',
      'time_type' => '24',
    ] + parent::defaultSettings();
  }

  /** * {@inheritdoc} */
  public function formElement(FieldItemListInterface $items$delta, array $element, array &$form, FormStateInterface $form_state) {
    $element = parent::formElement($items$delta$element$form$form_state);

    // Wrap all of the select elements with a fieldset.     $element['#theme_wrappers'][] = 'fieldset';

    $date_order = $this->getSetting('date_order');

    if ($this->getFieldSetting('datetime_type') == 'datetime') {
      $time_type = $this->getSetting('time_type');
      $increment = $this->getSetting('increment');
    }
    else {
      

class OptionsSelectWidget extends OptionsWidgetBase {

  /** * {@inheritdoc} */
  public function formElement(FieldItemListInterface $items$delta, array $element, array &$form, FormStateInterface $form_state) {
    $element = parent::formElement($items$delta$element$form$form_state);

    $element += [
      '#type' => 'select',
      '#options' => $this->getOptions($items->getEntity()),
      '#default_value' => $this->getSelectedOptions($items),
      // Do not display a 'multiple' select box if there is only one option.       '#multiple' => $this->multiple && count($this->options) > 1,
    ];

    return $element;
  }

  
use Drupal\datetime_range\Plugin\Field\FieldType\DateRangeItem;

/** * Base class for the 'daterange_*' widgets. */
class DateRangeWidgetBase extends DateTimeWidgetBase {

  /** * {@inheritdoc} */
  public function formElement(FieldItemListInterface $items$delta, array $element, array &$form, FormStateInterface $form_state) {
    $element = parent::formElement($items$delta$element$form$form_state);

    // Wrap all of the select elements with a fieldset.     $element['#theme_wrappers'][] = 'fieldset';

    $element['#element_validate'][] = [$this, 'validateStartEnd'];
    $element['value']['#title'] = $this->t('Start date');

    $element['end_value'] = [
      '#title' => $this->t('End date'),
    ] + $element['value'];

    
Home | Imprint | This part of the site doesn't use cookies.