You are here

public function CalendarEventTypeForm::form in Opigno calendar event 3.x

Same name and namespace in other branches
  1. 8 src/Form/CalendarEventTypeForm.php \Drupal\opigno_calendar_event\Form\CalendarEventTypeForm::form()

Throws

\Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException

Overrides EntityForm::form

File

src/Form/CalendarEventTypeForm.php, line 53

Class

CalendarEventTypeForm
Form handler for calendar event type forms.

Namespace

Drupal\opigno_calendar_event\Form

Code

public function form(array $form, FormStateInterface $form_state) {
  $form = parent::form($form, $form_state);

  /** @var \Drupal\opigno_calendar_event\Entity\CalendarEventType $type */
  $type = $this->entity;
  $form['label'] = [
    '#title' => $this
      ->t('Label'),
    '#type' => 'textfield',
    '#default_value' => $type
      ->label(),
    '#description' => $this
      ->t('The human-readable name of this calendar event type.'),
    '#required' => TRUE,
    '#size' => 30,
  ];
  $form['id'] = [
    '#type' => 'machine_name',
    '#default_value' => $type
      ->id(),
    '#maxlength' => EntityTypeInterface::BUNDLE_MAX_LENGTH,
    '#machine_name' => [
      'exists' => [
        'Drupal\\opigno_calendar_event\\Entity\\CalendarEventType',
        'load',
      ],
      'source' => [
        'label',
      ],
    ],
    '#description' => $this
      ->t('A unique machine-readable name for this calendar event type. It must only contain lowercase letters, numbers, and underscores.'),
  ];
  $form['description'] = [
    '#title' => $this
      ->t('Description'),
    '#type' => 'textarea',
    '#default_value' => $type
      ->get('description'),
    '#description' => $this
      ->t('This text will be displayed on the <em>Add new calendar event</em> page.'),
  ];
  $form['advanced'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Advanced'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
  ];
  $options = $this
    ->getDateFieldTypes();
  $default_value = $type
    ->isNew() ? key(array_reverse($options)) : $type
    ->get('date_field_type');

  /** @var \Drupal\opigno_calendar_event\CalendarEventStorage $storage */
  $storage = $this->entityTypeManager
    ->getStorage(CalendarEventInterface::ENTITY_TYPE_ID);

  // @todo Add a validation constraint for this once config entity validation
  //   is supported. See https://www.drupal.org/project/drupal/issues/1818574.
  $disabled = !$type
    ->isNew() && $storage
    ->hasBundleData($type
    ->id());
  $description = !$disabled ? $this
    ->t('Choose which kind of date this calendar event type will use: Drupal provides the <em>Date</em> and <em>Date range</em> types, contributed modules may define more.') : $this
    ->t('This setting cannot be changed because there is data for this calendar event type.');
  $form['advanced']['date_field_type'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Date type'),
    '#description' => $description,
    '#options' => $options,
    '#default_value' => $default_value,
    '#disabled' => $disabled,
  ];
  return $this
    ->protectBundleIdElement($form);
}