You are here

public function LogForm::form in Log entity 2.x

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

Gets the actual form array to be built.

Overrides ContentEntityForm::form

See also

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

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

File

src/Form/LogForm.php, line 72

Class

LogForm
Form controller for Log entities.

Namespace

Drupal\log\Form

Code

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

  /** @var \Drupal\log\Entity\LogInterface $log */
  $log = $this->entity;

  // Changed must be sent to the client, for later overwrite error checking.
  $form['changed'] = [
    '#type' => 'hidden',
    '#default_value' => $log
      ->getChangedTime(),
  ];
  $form += parent::form($form, $form_state);

  // Set autocomplete for log names.
  if ($this->moduleHandler
    ->moduleExists('views')) {
    $num_of_logs = $this->entityTypeManager
      ->getStorage('log')
      ->getQuery()
      ->condition('type', $log
      ->bundle())
      ->count()
      ->execute();
    if ($num_of_logs > 0) {
      $form['name']['widget'][0]['value']['#description'] = FieldFilteredMarkup::create($form['name']['widget'][0]['value']['#description'] . ' ' . $this
        ->t('As you type, frequently used log names will be suggested.'));
      $form['name']['widget'][0]['value']['#autocomplete_route_name'] = 'log.autocomplete.name';
      $form['name']['widget'][0]['value']['#autocomplete_route_parameters'] = [
        'log_bundle' => $log
          ->bundle(),
      ];
    }
  }
  $form['advanced']['#attributes']['class'][] = 'entity-meta';
  $form['meta'] = [
    '#type' => 'details',
    '#group' => 'advanced',
    '#weight' => -10,
    '#title' => $this
      ->t('Status'),
    '#attributes' => [
      'class' => [
        'entity-meta__header',
      ],
    ],
    '#tree' => TRUE,
    '#access' => $this->currentUser
      ->hasPermission('administer log'),
  ];
  $form['meta']['status'] = [
    '#type' => 'item',
    '#markup' => $log
      ->get('status')
      ->first()
      ->getLabel(),
    '#access' => !$log
      ->isNew(),
    '#$log' => [
      'class' => [
        'entity-meta__title',
      ],
    ],
  ];
  $form['meta']['changed'] = [
    '#type' => 'item',
    '#title' => $this
      ->t('Last saved'),
    '#markup' => !$log
      ->isNew() ? $this->dateFormatter
      ->format($log
      ->getChangedTime(), 'short') : $this
      ->t('Not saved yet'),
    '#wrapper_attributes' => [
      'class' => [
        'entity-meta__last-saved',
      ],
    ],
  ];
  $form['meta']['author'] = [
    '#type' => 'item',
    '#title' => $this
      ->t('Author'),
    '#markup' => $log
      ->getOwner()
      ->getAccountName(),
    '#wrapper_attributes' => [
      'class' => [
        'entity-meta__author',
      ],
    ],
  ];

  // Author information for administrators.
  $form['author'] = [
    '#type' => 'details',
    '#title' => t('Authoring information'),
    '#group' => 'advanced',
    '#weight' => 90,
    '#optional' => TRUE,
  ];
  if (isset($form['uid'])) {
    $form['uid']['#group'] = 'author';
  }
  if (isset($form['created'])) {
    $form['created']['#group'] = 'author';
  }
  $form['#attached']['library'][] = 'core/drupal.form';
  return $form;
}