You are here

public function Dimension::formElement in Dimension 2.1.x

Same name and namespace in other branches
  1. 8 src/Plugin/Field/FieldWidget/Dimension.php \Drupal\dimension\Plugin\Field\FieldWidget\Dimension::formElement()
  2. 2.0.x src/Plugin/Field/FieldWidget/Dimension.php \Drupal\dimension\Plugin\Field\FieldWidget\Dimension::formElement()

@noinspection UnsupportedStringOffsetOperationsInspection

Overrides NumberWidget::formElement

File

src/Plugin/Field/FieldWidget/Dimension.php, line 88

Class

Dimension

Namespace

Drupal\dimension\Plugin\Field\FieldWidget

Code

public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) : array {
  $default = $items[$delta]
    ->getFieldDefinition()
    ->getDefaultValue($items[$delta]
    ->getEntity());
  $element += [
    '#type' => 'fieldset',
  ];
  $arguments = [];

  /** @noinspection StaticInvocationViaThisInspection */
  foreach ($this
    ->fields() as $key => $label) {
    $settings = $this
      ->getSetting($key);
    $field_settings = $this
      ->getFieldSetting($key);
    $value = $items[$delta]->{$key} ?? $default[0][$key] ?? NULL;
    $arguments['fields'][$key] = [
      'scale' => $this
        ->getFieldSetting($key . '_scale'),
      'factor' => $field_settings['factor'],
    ];
    $element[$key] = [
      '#type' => 'number',
      '#title' => $settings['label'],
      '#default_value' => $value,
      '#placeholder' => $settings['placeholder'],
      '#step' => 0.1 ** $this
        ->getFieldSetting($key . '_scale'),
      '#description' => $settings['description'],
      '#attributes' => [
        'dimension-key' => $key,
      ],
    ];

    // Set minimum and maximum.
    if (is_numeric($field_settings['min'])) {
      $element[$key]['#min'] = $field_settings['min'];
    }
    if (is_numeric($field_settings['max'])) {
      $element[$key]['#max'] = $field_settings['max'];
    }

    // Add prefix and suffix.
    if ($field_settings['prefix']) {
      $prefixes = explode('|', $field_settings['prefix']);
      $element[$key]['#field_prefix'] = FieldFilteredMarkup::create(array_pop($prefixes));
    }
    if ($field_settings['suffix']) {
      $suffixes = explode('|', $field_settings['suffix']);
      $element[$key]['#field_suffix'] = FieldFilteredMarkup::create(array_pop($suffixes));
    }
  }
  $element['value'] = [
    '#type' => 'number',
    '#title' => $this
      ->t('Dimension'),
    '#default_value' => '',
    '#disabled' => TRUE,
    '#attributes' => [
      'dimension-key' => 'value',
    ],
  ];
  $field_settings = $this
    ->getFieldSetting('value');
  $arguments['value'] = [
    'scale' => $this
      ->getFieldSetting('value_scale'),
    'factor' => $field_settings['factor'],
  ];

  // Add prefix and suffix.
  if ($field_settings['prefix']) {
    $prefixes = explode('|', $field_settings['prefix']);
    $element['value']['#field_prefix'] = FieldFilteredMarkup::create(array_pop($prefixes));
  }
  if ($field_settings['suffix']) {
    $suffixes = explode('|', $field_settings['suffix']);
    $element['value']['#field_suffix'] = FieldFilteredMarkup::create(array_pop($suffixes));
  }
  $id = $this->fieldDefinition
    ->getConfig($items[$delta]
    ->getEntity()
    ->bundle())
    ->id();
  $element['#attached']['library'][] = 'dimension/widget';
  $element['#attached']['drupalSettings']['dimension'][$id] = $arguments;
  $element['#attributes']['dimension-id'] = $id;
  $element['#attributes']['class'][] = 'dimension-wrapper';
  return $element;
}