You are here

LogForm.php in Log entity 8

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

Contains \Drupal\log\Entity\Form\LogForm.

Namespace

Drupal\log\Form

File

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

/**
 * @file
 * Contains \Drupal\log\Entity\Form\LogForm.
 */
namespace Drupal\log\Form;

use Drupal\Core\Entity\ContentEntityForm;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Language\Language;

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

  /**
   * {@inheritdoc}
   */
  public function form(array $form, FormStateInterface $form_state) {

    /* @var $entity \Drupal\log\Entity\Log */
    $entity = $this->entity;
    $form['advanced'] = array(
      '#type' => 'vertical_tabs',
      '#attributes' => array(
        'class' => array(
          'entity-meta',
        ),
      ),
      '#weight' => 99,
    );
    $form['revision_information'] = array(
      '#type' => 'details',
      '#group' => 'advanced',
      '#title' => t('Revision information'),
      '#weight' => 20,
      '#optional' => TRUE,
    );
    $form['revision'] = array(
      '#title' => $this
        ->t('Revision'),
      '#type' => 'checkbox',
      '#default_value' => $entity->type->entity
        ->isNewRevision(),
      '#weight' => 99,
      '#group' => 'revision_information',
    );
    $form['author'] = array(
      '#type' => 'details',
      '#title' => t('Authoring information'),
      '#group' => 'advanced',
      '#weight' => 90,
      '#optional' => TRUE,
    );
    $form['langcode'] = array(
      '#title' => $this
        ->t('Language'),
      '#type' => 'language_select',
      '#default_value' => $entity->langcode->value,
      '#languages' => Language::STATE_ALL,
      '#group' => 'author',
    );
    $form = parent::form($form, $form_state);
    if (isset($form['user_id'])) {
      $form['user_id']['#group'] = 'author';
    }
    if (isset($form['created'])) {
      $form['created']['#group'] = 'author';
    }
    if (isset($form['name'])) {
      $form['name']['#access'] = $entity->type->entity
        ->isNameEditable();
    }
    return $form;
  }

  /**
   * {@inheritdoc}
   *
   * Updates the log object by processing the submitted values.
   *
   * This function can be called by a "Next" button of a wizard to update the
   * form state's entity with the current step's values before proceeding to the
   * next step.
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {

    // Build the log object from the submitted values.
    parent::submitForm($form, $form_state);
    $entity = $this->entity;

    // Save as a new revision if requested to do so.
    if (!$form_state
      ->isValueEmpty('revision') && $form_state
      ->getValue('revision') != FALSE) {
      $entity
        ->setNewRevision();

      // If a new revision is created, save the current user as revision author.
      $entity
        ->setRevisionCreationTime(REQUEST_TIME);
      $entity
        ->setRevisionAuthorId(\Drupal::currentUser()
        ->id());
    }
    else {
      $entity
        ->setNewRevision(FALSE);
    }
  }

  /**
   * {@inheritdoc}
   */
  public function save(array $form, FormStateInterface $form_state) {
    $entity = $this->entity;
    $status = $entity
      ->save();
    switch ($status) {
      case SAVED_NEW:
        drupal_set_message($this
          ->t('Created the %label Log.', [
          '%label' => $entity
            ->label(),
        ]));
        break;
      default:
        drupal_set_message($this
          ->t('Saved the %label Log.', [
          '%label' => $entity
            ->label(),
        ]));
    }
    $form_state
      ->setRedirectUrl($entity
      ->toUrl('canonical'));
  }

}

Classes

Namesort descending Description
LogForm Form controller for Log edit forms.