<?php

namespace Drupal\{{ machine_name }}\Plugin\Field\FieldWidget;

{% sort %}
  {% if datetime %}
use Drupal\Core\Datetime\DrupalDateTime;
  {% endif %}
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\WidgetBase;
use Drupal\Core\Form\FormStateInterface;
use Symfony\Component\Validator\ConstraintViolationInterface;
  {% if list %}
use Drupal\{{ machine_name }}\Plugin\Field\FieldType\{{ type_class }};
  {% endif %}
{% endsort %}

/**
 * Defines the '{{ field_id }}' field widget.
 *
 * @FieldWidget(
 *   id = "{{ field_id }}",
 *   label = @Translation("{{ field_label }}"),
 *   field_types = {"{{ field_id }}"},
 * )
 */
class {{ widget_class }} extends WidgetBase {

{% if widget_settings %}
  /**
   * {@inheritdoc}
   */
  public static function defaultSettings() {
    return ['foo' => 'bar'] + parent::defaultSettings();
  }

  /**
   * {@inheritdoc}
   */
  public function settingsForm(array $form, FormStateInterface $form_state) {
    $settings = $this->getSettings();
    $element['foo'] = [
      '#type' => 'textfield',
      '#title' => $this->t('Foo'),
      '#default_value' => $settings['foo'],
    ];
    return $element;
  }

  /**
   * {@inheritdoc}
   */
  public function settingsSummary() {
    $settings = $this->getSettings();
    $summary[] = $this->t('Foo: @foo', ['@foo' => $settings['foo']]);
    return $summary;
  }

{% endif %}
  /**
   * {@inheritdoc}
   */
  public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {

{% for subfield in subfields %}
  {% set title %}'#title' => $this->t('{{ subfield.name }}'),{% endset %}
  {% set default_value %}'#default_value' => isset($items[$delta]->{{ subfield.machine_name }}) ? $items[$delta]->{{ subfield.machine_name }} : NULL,{% endset %}
  {% set size %}'#size' => 20,{% endset %}
  {% if subfield.list %}
    $element['{{ subfield.machine_name }}'] = [
      '#type' => 'select',
      {{ title }}
      '#options' => ['' => $this->t('- {{ subfield.required ? 'Select a value' : 'None' }} -')] + {{ type_class }}::{{ subfield.allowed_values_method }}(),
      {{ default_value }}
    ];
  {% else %}
    {% if subfield.type == 'boolean' %}
    $element['{{ subfield.machine_name }}'] = [
      '#type' => 'checkbox',
      {{ title }}
      {{ default_value }}
    ];
    {% elseif subfield.type == 'string' %}
    $element['{{ subfield.machine_name }}'] = [
      '#type' => 'textfield',
      {{ title }}
      {{ default_value }}
      {% if inline %}
      {{ size }}
      {% endif %}
    ];
    {% elseif subfield.type == 'text' %}
    $element['{{ subfield.machine_name }}'] = [
      '#type' => 'textarea',
      {{ title }}
      {{ default_value }}
    ];
    {% elseif subfield.type == 'integer' %}
    $element['{{ subfield.machine_name }}'] = [
      '#type' => 'number',
      {{ title }}
      {{ default_value }}
    ];
    {% elseif subfield.type == 'float' %}
    $element['{{ subfield.machine_name }}'] = [
      '#type' => 'number',
      {{ title }}
      {{ default_value }}
      '#step' => 0.001,
    ];
    {% elseif subfield.type == 'numeric' %}
    $element['{{ subfield.machine_name }}'] = [
      '#type' => 'number',
      {{ title }}
      {{ default_value }}
      '#step' => 0.01,
    ];
    {% elseif subfield.type == 'email' %}
    $element['{{ subfield.machine_name }}'] = [
      '#type' => 'email',
      {{ title }}
      {{ default_value }}
      {% if inline %}
      {{ size }}
      {% endif %}
    ];
    {% elseif subfield.type == 'telephone' %}
    $element['{{ subfield.machine_name }}'] = [
      '#type' => 'tel',
      {{ title }}
      {{ default_value }}
      {% if inline %}
      {{ size }}
      {% endif %}
    ];
    {% elseif subfield.type == 'uri' %}
    $element['{{ subfield.machine_name }}'] = [
      '#type' => 'url',
      {{ title }}
      {{ default_value }}
      {% if inline %}
      {{ size }}
      {% endif %}
    ];
    {% elseif subfield.type == 'datetime' %}
    $element['{{ subfield.machine_name }}'] = [
      '#type' => 'datetime',
      {{ title }}
      '#default_value' => NULL,
      {% if subfield.date_type == 'date' %}
      '#date_time_element' => 'none',
      '#date_time_format' => '',
      {% endif %}
    ];
    if (isset($items[$delta]->{{ subfield.machine_name }})) {
      $element['{{ subfield.machine_name }}']['#default_value'] = DrupalDateTime::createFromFormat(
        '{{ subfield.date_storage_format }}',
        $items[$delta]->{{ subfield.machine_name }},
        'UTC'
      );
    }
    {% endif %}
  {% endif %}

{% endfor %}
    $element['#theme_wrappers'] = ['container', 'form_element'];
{% if inline %}
    $element['#attributes']['class'][] = 'container-inline';
{% endif %}
    $element['#attributes']['class'][] = '{{ field_id|u2h }}-elements';
    $element['#attached']['library'][] = '{{ machine_name }}/{{ field_id }}';

    return $element;
  }

  /**
   * {@inheritdoc}
   */
  public function errorElement(array $element, ConstraintViolationInterface $violation, array $form, FormStateInterface $form_state) {
    return isset($violation->arrayPropertyPath[0]) ? $element[$violation->arrayPropertyPath[0]] : $element;
  }

  /**
   * {@inheritdoc}
   */
  public function massageFormValues(array $values, array $form, FormStateInterface $form_state) {
    foreach ($values as $delta => $value) {
{% for subfield in subfields %}
      if ($value['{{ subfield.machine_name }}'] === '') {
        $values[$delta]['{{ subfield.machine_name }}'] = NULL;
      }
  {% if subfield.type == 'datetime' %}
      if ($value['{{ subfield.machine_name }}'] instanceof DrupalDateTime) {
        $values[$delta]['{{ subfield.machine_name }}'] = $value['{{ subfield.machine_name }}']->format('{{ subfield.date_storage_format }}');
      }
  {% endif %}
{% endfor %}
    }
    return $values;
  }

}
