You are here

public function EntityExtraFieldForm::form in Entity Extra Field 8

Same name and namespace in other branches
  1. 2.0.x src/Form/EntityExtraFieldForm.php \Drupal\entity_extra_field\Form\EntityExtraFieldForm::form()

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/EntityExtraFieldForm.php, line 94

Class

EntityExtraFieldForm
Define entity extra field form.

Namespace

Drupal\entity_extra_field\Form

Code

public function form(array $form, FormStateInterface $form_state) {

  /** @var \Drupal\entity_extra_field\Entity\EntityExtraField $entity */
  $entity = $this->entity;
  $form = parent::form($form, $form_state);
  $form['#parents'] = [];
  $form['#prefix'] = '<div id="entity-extra-field">';
  $form['#suffix'] = '</div>';
  $form['label'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Field Name'),
    '#maxlength' => 255,
    '#default_value' => $entity
      ->label(),
    '#description' => $this
      ->t('Input the extra field name.'),
    '#required' => TRUE,
  ];
  $form['name'] = [
    '#type' => 'machine_name',
    '#machine_name' => [
      'exists' => [
        $entity,
        'exists',
      ],
    ],
    '#disabled' => !$entity
      ->isNew(),
    '#default_value' => $entity
      ->name(),
  ];
  $form['display_label'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Display Label'),
    '#description' => $this
      ->t('Display the extra field name.'),
    '#default_value' => $entity
      ->displayLabel(),
  ];
  $form['display'] = [
    '#type' => 'container',
    '#tree' => TRUE,
  ];
  $form['display']['type'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Display Type'),
    '#description' => $this
      ->t('Select the extra field display type. <br/>
        The <em>View</em> display will render within the entity view. <br/>
        The <em>Form</em> display will render within the entity edit form.'),
    '#required' => TRUE,
    '#options' => [
      'form' => $this
        ->t('Form'),
      'view' => $this
        ->t('View'),
    ],
    '#empty_option' => $this
      ->t('- Select -'),
    '#default_value' => $this
      ->getEntityFormStateValue([
      'display',
      'type',
    ], $form_state),
  ];
  $form['description'] = [
    '#type' => 'textarea',
    '#title' => $this
      ->t('Description'),
    '#default_value' => $entity
      ->description(),
  ];
  $field_type_id = $this
    ->getEntityFormStateValue('field_type_id', $form_state);
  $form['field_type_id'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Field Type'),
    '#required' => TRUE,
    '#options' => $this
      ->getExtraFieldTypeOptions(),
    '#empty_option' => $this
      ->t('- Select -'),
    '#default_value' => $field_type_id,
    '#ajax' => [
      'event' => 'change',
      'method' => 'replace',
      'wrapper' => 'entity-extra-field',
      'callback' => [
        $this,
        'entityExtraFieldAjax',
      ],
    ],
  ];
  if (isset($field_type_id) && !empty($field_type_id)) {
    $field_type_instance = $this
      ->createFieldTypeInstance($field_type_id, $form_state);
    if ($field_type_instance !== FALSE && $field_type_instance instanceof PluginFormInterface) {
      $subform = [
        '#parents' => [
          'field_type_config',
        ],
      ];
      $form['field_type_config'] = [
        '#type' => 'fieldset',
        '#title' => $this
          ->t('Field Type Configuration'),
        '#tree' => TRUE,
      ];
      $form['field_type_config'] += $field_type_instance
        ->buildConfigurationForm($subform, SubformState::createForSubform($subform, $form, $form_state));
    }
  }
  $this
    ->attachFieldTypeConditionForm($form, $form_state, ContextDefinition::create("entity:{$this->getExtraFieldBaseEntityTypeId()}"));
  return $form;
}