You are here

public function EckEntityBundleForm::form in Entity Construction Kit (ECK) 8

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/EntityBundle/EckEntityBundleForm.php, line 61

Class

EckEntityBundleForm
Form controller for ECK entity bundle forms.

Namespace

Drupal\eck\Form\EntityBundle

Code

public function form(array $form, FormStateInterface $form_state) {
  $form = parent::form($form, $form_state);
  $entity_type_id = $this->entity
    ->getEntityType()
    ->getBundleOf();
  $type = $this->entity;
  $entity = $this->entityTypeManager
    ->getStorage($entity_type_id)
    ->create([
    'type' => $this->operation == 'add' ? $type
      ->uuid() : $type
      ->id(),
  ]);
  $type_label = $entity
    ->getEntityType()
    ->getLabel();
  $form['name'] = [
    '#title' => $this
      ->t('Name'),
    '#type' => 'textfield',
    '#default_value' => $type->name,
    '#description' => $this
      ->t('The human-readable name of this entity bundle. This text will be displayed as part of the list on the <em>Add @type content</em> page. This name must be unique.', [
      '@type' => $type_label,
    ]),
    '#required' => TRUE,
    '#size' => 30,
  ];
  $form['type'] = [
    '#type' => 'machine_name',
    '#default_value' => $type
      ->id(),
    '#maxlength' => EntityTypeInterface::BUNDLE_MAX_LENGTH,
    '#disabled' => $type
      ->isLocked(),
    '#machine_name' => [
      'exists' => [
        $this,
        'exists',
      ],
      'source' => [
        'name',
      ],
    ],
    '#description' => $this
      ->t('A unique machine-readable name for this entity type bundle. It must only contain lowercase letters, numbers, and underscores. This name will be used for constructing the URL of the Add %type content page, in which underscores will be converted into hyphens.', [
      '%type' => $type_label,
    ]),
  ];
  $form['description'] = [
    '#title' => $this
      ->t('Description'),
    '#type' => 'textarea',
    '#default_value' => $type->description,
    '#description' => $this
      ->t('Describe this entity type bundle. The text will be displayed on the <em>Add @type content</em> page.', [
      '@type' => $type_label,
    ]),
  ];

  // Field title overrides.
  $entity_type_config = \Drupal::config('eck.eck_entity_type.' . $entity_type_id);
  $base_fields = $this->entityFieldManager
    ->getBaseFieldDefinitions($type
    ->getEntityType()
    ->getBundleOf());
  $bundle_overrides = [];
  if ($type
    ->id() !== NULL) {
    $bundle_overrides = $this->entityFieldManager
      ->getFieldDefinitions($entity_type_id, $type
      ->id());
  }
  foreach ([
    'title',
    'uid',
    'created',
    'changed',
    'status',
  ] as $field) {
    if (!empty($entity_type_config
      ->get($field))) {
      if (!isset($form['field_overrides'])) {
        $form['field_overrides'] = [
          '#type' => 'details',
          '#title' => $this
            ->t('Base field title and description overrides'),
          '#open' => FALSE,
        ];
      }
      $form['field_overrides'][$field] = [
        '#type' => 'fieldset',
        '#title' => $base_fields[$field]
          ->getLabel(),
        '#tree' => FALSE,
      ];
      $fieldset =& $form['field_overrides'][$field];
      if (isset($bundle_overrides[$field])) {
        $title_override = $bundle_overrides[$field]
          ->getLabel();
        if ($title_override === $base_fields[$field]
          ->getLabel()) {
          unset($title_override);
        }
      }
      $fieldset[$field . '_title_override'] = [
        '#type' => 'textfield',
        '#title' => $this
          ->t('Title'),
        '#description' => $this
          ->t('New title for the base @title field.', [
          '@title' => $field,
        ]),
        '#default_value' => $title_override ?? '',
      ];
      if (isset($bundle_overrides[$field])) {
        $description_override = $bundle_overrides[$field]
          ->getDescription();
        if ($description_override === $base_fields[$field]
          ->getDescription()) {
          unset($description_override);
        }
      }
      $fieldset[$field . '_description_override'] = [
        '#type' => 'textfield',
        '#title' => $this
          ->t('Description'),
        '#description' => $this
          ->t('New description for the base @title field. Enter %none to hide the default description.', [
          '@title' => $field,
          '%none' => '<none>',
        ]),
        '#default_value' => $description_override ?? '',
      ];
    }
  }
  return $form;
}