You are here

LogForm.php in Log entity 2.x

Same filename and directory in other branches
  1. 8 src/Form/LogForm.php

Namespace

Drupal\log\Form

File

src/Form/LogForm.php
View source
<?php

namespace Drupal\log\Form;

use Drupal\Component\Datetime\TimeInterface;
use Drupal\Core\Datetime\DateFormatterInterface;
use Drupal\Core\Entity\ContentEntityForm;
use Drupal\Core\Entity\EntityRepositoryInterface;
use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
use Drupal\Core\Field\FieldFilteredMarkup;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Session\AccountInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Form controller for Log entities.
 *
 * @ingroup log
 */
class LogForm extends ContentEntityForm {

  /**
   * The Current User object.
   *
   * @var \Drupal\Core\Session\AccountInterface
   */
  protected $currentUser;

  /**
   * The date formatter service.
   *
   * @var \Drupal\Core\Datetime\DateFormatterInterface
   */
  protected $dateFormatter;

  /**
   * Constructs a LogForm object.
   *
   * @param \Drupal\Core\Entity\EntityRepositoryInterface $entity_repository
   *   The entity repository.
   * @param \Drupal\Core\Entity\EntityTypeBundleInfoInterface $entity_type_bundle_info
   *   The entity type bundle service.
   * @param \Drupal\Component\Datetime\TimeInterface $time
   *   The time service.
   * @param \Drupal\Core\Session\AccountInterface $current_user
   *   The current user.
   * @param \Drupal\Core\Datetime\DateFormatterInterface $date_formatter
   *   The date formatter service.
   */
  public function __construct(EntityRepositoryInterface $entity_repository, EntityTypeBundleInfoInterface $entity_type_bundle_info = NULL, TimeInterface $time = NULL, AccountInterface $current_user, DateFormatterInterface $date_formatter) {
    parent::__construct($entity_repository, $entity_type_bundle_info, $time);
    $this->currentUser = $current_user;
    $this->dateFormatter = $date_formatter;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('entity.repository'), $container
      ->get('entity_type.bundle.info'), $container
      ->get('datetime.time'), $container
      ->get('current_user'), $container
      ->get('date.formatter'));
  }

  /**
   * {@inheritdoc}
   */
  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;
  }

  /**
   * {@inheritdoc}
   */
  public function save(array $form, FormStateInterface $form_state) {
    parent::save($form, $form_state);
    $entity_url = $this->entity
      ->toUrl()
      ->setAbsolute()
      ->toString();
    $this
      ->messenger()
      ->addMessage($this
      ->t('Saved log: <a href=":url">%label</a>', [
      ':url' => $entity_url,
      '%label' => $this->entity
        ->label(),
    ]));
    $form_state
      ->setRedirectUrl($this->entity
      ->toUrl());
  }

}

Classes

Namesort descending Description
LogForm Form controller for Log entities.