You are here

public function UnitForm::buildForm in Booking and Availability Management Tools for Drupal 8

Form constructor.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Return value

array The form structure.

Overrides EntityForm::buildForm

File

modules/bat_unit/src/Entity/Form/UnitForm.php, line 65
Contains \Drupal\bat_unit\Entity\Form\UnitForm.

Class

UnitForm
Form controller for Unit edit forms.

Namespace

Drupal\bat_unit\Entity\Form

Code

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

  /* @var $entity \Drupal\bat\Entity\Unit */
  $form = parent::buildForm($form, $form_state);
  $entity = $this->entity;
  $form['changed'] = [
    '#type' => 'hidden',
    '#default_value' => $entity
      ->getChangedTime(),
  ];
  $form['#theme'] = [
    'bat_entity_edit_form',
  ];
  $form['#attached']['library'][] = 'bat/bat_ui';
  $form['advanced'] = [
    '#type' => 'container',
    '#attributes' => [
      'class' => [
        'entity-meta',
      ],
    ],
    '#weight' => 99,
  ];
  $is_new = !$entity
    ->isNew() ? $this->dateFormatter
    ->format($entity
    ->getChangedTime(), 'short') : t('Not saved yet');
  $form['meta'] = [
    '#attributes' => [
      'class' => [
        'entity-meta__header',
      ],
    ],
    '#type' => 'container',
    '#group' => 'advanced',
    '#weight' => -100,
    'published' => [
      '#type' => 'html_tag',
      '#tag' => 'h3',
      '#value' => $entity
        ->getStatus() ? t('Published') : t('Not published'),
      '#access' => !$entity
        ->isNew(),
      '#attributes' => [
        'class' => 'entity-meta__title',
      ],
    ],
    'changed' => [
      '#type' => 'item',
      '#wrapper_attributes' => [
        'class' => [
          'entity-meta__last-saved',
          'container-inline',
        ],
      ],
      '#markup' => '<h4 class="label inline">' . t('Last saved') . '</h4> ' . $is_new,
    ],
    'author' => [
      '#type' => 'item',
      '#wrapper_attributes' => [
        'class' => [
          'author',
          'container-inline',
        ],
      ],
      '#markup' => '<h4 class="label inline">' . t('Author') . '</h4> ' . $entity
        ->getOwner()
        ->getDisplayName(),
    ],
  ];
  $form['author'] = [
    '#type' => 'details',
    '#title' => t('Authoring information'),
    '#group' => 'advanced',
    '#attributes' => [
      'class' => [
        'type-form-author',
      ],
    ],
    '#weight' => 90,
    '#optional' => TRUE,
    '#open' => TRUE,
  ];
  if (isset($form['uid'])) {
    $form['uid']['#group'] = 'author';
  }
  if (isset($form['created'])) {
    $form['created']['#group'] = 'author';
  }
  $form['#entity_builders']['update_status'] = [
    $this,
    'updateStatus',
  ];
  return $form;
}