You are here

public function KeyValueWidgetTrait::formElement in Key value field 8

File

src/Plugin/Field/FieldWidget/KeyValueWidgetTrait.php, line 155

Class

KeyValueWidgetTrait
Common traits for key value field widgets inheriting from different widgets.

Namespace

Drupal\key_value_field\Plugin\Field\FieldWidget

Code

public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {

  // Get the textfield form element.
  $element = parent::formElement($items, $delta, $element, $form, $form_state);

  // Check for an empty format on formattable items.
  if (array_key_exists('#format', $element) && empty($element['#format'])) {

    // Get the default format.
    $default_format = $this
      ->getFieldSetting('default_format');

    // Get the formats available to the current user.
    $available_formats = filter_formats();

    // Set the format to the default if empty.
    if (!empty($default_format) && array_key_exists($default_format, $available_formats)) {
      $element['#format'] = $default_format;
    }
  }

  // Grab settings for all.
  $key_size = $this
    ->getSetting('key_size');
  $key_label = $this
    ->getSetting('key_label');
  $value_label = $this
    ->getSetting('value_label');
  $key_placeholder = $this
    ->getSetting('key_placeholder');
  $description_enabled = $this
    ->getSetting('description_enabled');
  $description_label = $this
    ->getSetting('description_label');
  $description_rows = $this
    ->getSetting('description_rows');
  $description_placeholder = $this
    ->getSetting('description_placeholder');

  // Display the title for key_value fields.
  if (isset($element['value']) && array_key_exists('#title_display', $element['value'])) {
    unset($element['value']['#title_display']);
    $element['value']['#title'] = !empty($value_label) ? $value_label : $this
      ->t('Value');
  }

  // Create a description field if it is enabled.
  $description = !$description_enabled ? [] : [
    '#title' => $description_label,
    '#type' => 'textarea',
    '#default_value' => isset($items[$delta]->value) ? $items[$delta]->description : NULL,
    '#placeholder' => $description_placeholder,
    '#maxlength' => 255,
    '#rows' => $description_rows,
    '#weight' => 2,
    '#attributes' => [
      'class' => [
        'js-text-full',
        'text-full',
        'key-value-widget-description',
      ],
    ],
  ];

  // Add the key field.
  $key_field = [
    'key' => [
      '#title' => $key_label,
      '#type' => 'textfield',
      '#default_value' => isset($items[$delta]->key) ? $items[$delta]->key : NULL,
      '#size' => $key_size,
      '#placeholder' => $key_placeholder,
      '#maxlength' => $this
        ->getFieldSetting('key_max_length'),
      '#attributes' => [
        'class' => [
          'js-text-full',
          'text-full',
          'key-value-widget-key',
        ],
      ],
      '#weight' => -1,
    ],
    // Add the description field.
    'description' => $description,
    // Add a class to the widget form.
    '#attributes' => [
      'class' => [
        'key-value-widget',
        'js-text-full',
        'text-full',
      ],
    ],
    '#title' => $value_label,
    '#title_display' => 'before',
    '#element_validate' => [
      [
        get_called_class(),
        'validateKeyElement',
      ],
    ],
  ];

  // Add the textarea form.
  $build = $key_field + $element;

  // Make the key dynamically required if value is not empty.
  // Only do this if both are initially empty. This avoids a confusing *
  // appearing on a field which is already filled in which makes some users
  // think that the whole field is required.
  if (empty($build['value']['#default_value']) && empty($build['key']['#default_value'])) {
    $build['key']['#states'] = [
      'required' => [
        ':input[name="' . $this->fieldDefinition
          ->getName() . '[' . $delta . '][value]"]' => [
          'empty' => FALSE,
        ],
      ],
    ];
  }
  return $build;
}