You are here

AuthorPaneFormBase.php in Author Pane 8.3

File

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

/**
 * @file
 * Contains \Drupal\author_pane\Form\AuthorPaneFormBase.
 */
namespace Drupal\author_pane\Form;

use Drupal\Core\Entity\EntityForm;
use Drupal\Core\Entity\Query\QueryFactory;
use Drupal\Core\Form\FormStateInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Class AuthorPaneFormBase.
 *
 * Base class for add and edit forms.
 */
class AuthorPaneFormBase extends EntityForm {

  /**
   * @var \Drupal\Core\Entity\Query\QueryFactory
   */
  protected $entityQueryFactory;

  /**
   * Construct the AuthorPaneFormBase.
   *
   * @param \Drupal\Core\Entity\Query\QueryFactory $query_factory
   *   An entity query factory for the AuthorPane entity type.
   */
  public function __construct(QueryFactory $query_factory) {
    $this->entityQueryFactory = $query_factory;
  }

  /**
   * Factory method for AuthorPaneFormBase.
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('entity.query'));
  }

  /**
   * Overrides Drupal\Core\Entity\EntityFormController::form().
   *
   * Builds the entity add/edit form.
   *
   * @param array $form
   *   An associative array containing the structure of the form.
   * @param array $form_state
   *   An associative array containing the current state of the form.
   *
   * @return array
   *   An associative array containing the AuthorPane add/edit form.
   */
  public function buildForm(array $form, FormStateInterface $form_state) {

    // Get anything we need from the base class.
    $form = parent::buildForm($form, $form_state);

    /** @var \Drupal\author_pane\Entity\AuthorPane $authorPane */
    $authorPane = $this->entity;

    // The Author Pane label.
    $form['label'] = array(
      '#type' => 'textfield',
      '#title' => $this
        ->t('Label'),
      '#maxlength' => 255,
      '#default_value' => $authorPane
        ->label(),
      '#description' => $this
        ->t("Author Pane label."),
      '#required' => TRUE,
    );

    // The Author Pane machine name.
    $form['id'] = array(
      '#type' => 'machine_name',
      '#title' => $this
        ->t('Machine name'),
      '#default_value' => $authorPane
        ->id(),
      '#machine_name' => array(
        'exists' => array(
          $this,
          'exists',
        ),
        'replace_pattern' => '([^a-z0-9_]+)|(^custom$)',
        'error' => 'The machine-readable name must be unique, and can only contain lowercase letters, numbers, and underscores. Additionally, it can not be the reserved word "custom".',
      ),
      '#disabled' => !$authorPane
        ->isNew(),
    );

    // The Author Pane description.
    $form['description'] = array(
      '#type' => 'textfield',
      '#title' => $this
        ->t('Description'),
      '#maxlength' => 255,
      '#default_value' => $authorPane
        ->getDescription(),
      '#description' => $this
        ->t("Notes about how or where this Author Pane will be used."),
      '#required' => FALSE,
    );
    return $form;
  }

  /**
   * Checks for an existing AuthorPane.
   *
   * @param string|int $entity_id
   *   The entity ID.
   * @param array $element
   *   The form element.
   * @param FormStateInterface $form_state
   *   The form state.
   *
   * @return bool
   *   TRUE if this AuthorPane already exists, FALSE otherwise.
   */
  public function exists($entity_id, array $element, FormStateInterface $form_state) {

    // Use the query factory to build a new robot entity query.
    $query = $this->entityQueryFactory
      ->get('author_pane');

    // Query the entity ID to see if its in use.
    $result = $query
      ->condition('id', $element['#field_prefix'] . $entity_id)
      ->execute();

    // We don't need to return the ID, only if it exists or not.
    return (bool) $result;
  }

  /**
   * Overrides Drupal\Core\Entity\EntityFormController::actions().
   *
   * To set the submit button text, we need to override actions().
   *
   * @param array $form
   *   An associative array containing the structure of the form.
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   An associative array containing the current state of the form.
   *
   * @return array
   *   An array of supported actions for the current entity form.
   */
  protected function actions(array $form, FormStateInterface $form_state) {

    // Get the basic actins from the base class.
    $actions = parent::actions($form, $form_state);

    // Change the submit button text.
    $actions['submit']['#value'] = $this
      ->t('Save');

    // Return the result.
    return $actions;
  }

  /**
   * Overrides Drupal\Core\Entity\EntityFormController::validate().
   *
   * @param array $form
   *   An associative array containing the structure of the form.
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   An associative array containing the current state of the form.
   */
  public function validate(array $form, FormStateInterface $form_state) {
    parent::validate($form, $form_state);

    // Add code here to validate your config entity's form elements.
    // Nothing to do here.
  }

  /**
   * Overrides Drupal\Core\Entity\EntityFormController::save().
   *
   * @param array $form
   *   An associative array containing the structure of the form.
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   An associative array containing the current state of the form.
   */
  public function save(array $form, FormStateInterface $form_state) {
    $authorPane = $this
      ->getEntity();
    $status = $authorPane
      ->save();

    // Grab the URL of the new entity. We'll use it in the message.
    $url = $authorPane
      ->urlInfo();

    // Create an edit link.
    $edit_link = $this
      ->l($this
      ->t('Edit'), $url);
    if ($status == SAVED_UPDATED) {

      // If we edited an existing entity...
      drupal_set_message($this
        ->t('AuthorPane %label has been updated.', array(
        '%label' => $authorPane
          ->label(),
      )));
      $this
        ->logger('contact')
        ->notice('AuthorPane %label has been updated.', [
        '%label' => $authorPane
          ->label(),
        'link' => $edit_link,
      ]);
    }
    else {

      // If we created a new entity...
      drupal_set_message($this
        ->t('AuthorPane %label has been added.', array(
        '%label' => $authorPane
          ->label(),
      )));
      $this
        ->logger('contact')
        ->notice('AuthorPane %label has been added.', [
        '%label' => $authorPane
          ->label(),
        'link' => $edit_link,
      ]);
    }

    // Redirect the user back to the listing route after the save operation.
    $form_state
      ->setRedirect('entity.author_pane.list');
  }

}

Classes

Namesort descending Description
AuthorPaneFormBase Class AuthorPaneFormBase.