You are here

public function FormatterForm::form in Custom Formatters 8.3

Gets the actual form array to be built.

Overrides EntityForm::form

See also

\Drupal\Core\Entity\EntityForm::processForm()

\Drupal\Core\Entity\EntityForm::afterBuild()

File

src/Form/FormatterForm.php, line 69

Class

FormatterForm
Form controller for the shortcut set entity edit forms.

Namespace

Drupal\custom_formatters\Form

Code

public function form(array $form, FormStateInterface $form_state) {
  $formatter_type = $this->entity
    ->getFormatterType();
  $form = parent::form($form, $form_state);

  // Show warning if formatter is currently in use.
  $dependent_entities = $this->entity
    ->getDependentEntities();
  if ($dependent_entities) {
    $form['warning'] = [
      '#theme' => 'status_messages',
      '#message_list' => [
        'warning' => [
          $this
            ->t("Changing the field type(s) are currently disabled as this formatter is required by the following configuration(s): @config", [
            '@config' => $this
              ->getDependentEntitiesList($dependent_entities),
          ]),
        ],
      ],
      '#status_headings' => [
        'warning' => t('Warning message'),
      ],
    ];
  }
  $form['label'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Formatter name'),
    '#description' => $this
      ->t('This will appear in the administrative interface to easily identify it.'),
    '#required' => TRUE,
    '#default_value' => $this->entity
      ->label(),
  ];
  $form['id'] = [
    '#type' => 'machine_name',
    '#machine_name' => [
      'exists' => '\\Drupal\\custom_formatters\\Entity\\Formatter::load',
      'source' => [
        'label',
      ],
      'replace_pattern' => '[^a-z0-9_]+',
      'replace' => '_',
    ],
    '#default_value' => $this->entity
      ->isNew() ? NULL : $this->entity
      ->id(),
    '#disabled' => !$this->entity
      ->isNew(),
    '#maxlength' => 255,
  ];
  $form['type'] = [
    '#type' => 'value',
    '#value' => $this->entity
      ->get('type'),
  ];
  $form['status'] = [
    '#type' => 'value',
    '#value' => TRUE,
  ];
  $form['description'] = [
    '#type' => 'textarea',
    '#title' => $this
      ->t('Description'),
    '#default_value' => $this->entity
      ->get('description'),
  ];
  $form['field_types'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Field type(s)'),
    '#options' => $this
      ->getFieldTypes(),
    '#default_value' => $this->entity
      ->get('field_types'),
    '#required' => TRUE,
    '#multiple' => $formatter_type
      ->getPluginDefinition()['multipleFields'],
    '#ajax' => [
      'callback' => '::formAjax',
      'wrapper' => 'plugin-wrapper',
    ],
    '#disabled' => $dependent_entities,
  ];

  // Get Formatter type settings form.
  $plugin_form = [];
  $form['plugin'] = $formatter_type
    ->settingsForm($plugin_form, $form_state);
  $form['plugin']['#type'] = 'container';
  $form['plugin']['#prefix'] = "<div id='plugin-wrapper'>";
  $form['plugin']['#suffix'] = "</div>";

  // Third party integration settings form.
  $extras = $this
    ->getFormatterExtrasForm();
  if ($extras && is_array($extras)) {
    $form['vertical_tabs'] = [
      '#type' => 'vertical_tabs',
      '#title' => $this
        ->t('Extras'),
      '#parents' => [
        'extras',
      ],
    ];
    $form['extras'] = $extras;
    $form['extras']['#tree'] = TRUE;
  }
  return $form;
}