You are here

VoteTypeForm.php in Voting API 8.3

Namespace

Drupal\votingapi

File

src/VoteTypeForm.php
View source
<?php

namespace Drupal\votingapi;

use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Entity\EntityForm;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Form\FormStateInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Form controller for vote type forms.
 */
class VoteTypeForm extends EntityForm {

  /**
   * The entity manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * The entity field manager.
   *
   * @var \Drupal\Core\Entity\EntityFieldManagerInterface
   */
  protected $entityFieldManager;

  /**
   * Constructs the VoteTypeForm object.
   *
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_manager
   *   The entity manager.
   * @param \Drupal\Core\Entity\EntityFieldManagerInterface $entity_field_manager
   *   The entity field manager.
   */
  public function __construct(EntityTypeManagerInterface $entity_manager, EntityFieldManagerInterface $entity_field_manager) {
    $this->entityTypeManager = $entity_manager;
    $this->entityFieldManager = $entity_field_manager;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('entity_type.manager'), $container
      ->get('entity_field.manager'));
  }

  /**
   * {@inheritdoc}
   */
  public function form(array $form, FormStateInterface $form_state) {
    $form = parent::form($form, $form_state);
    $type = $this->entity;
    if ($this->operation == 'add') {
      $form['#title'] = $this
        ->t('Add vote type');
    }
    else {
      $form['#title'] = $this
        ->t('Edit %label vote type', [
        '%label' => $type
          ->label(),
      ]);
    }
    $form['label'] = [
      '#title' => $this
        ->t('Name'),
      '#type' => 'textfield',
      '#default_value' => $type
        ->label(),
      '#description' => $this
        ->t('The human-readable name of this vote type. This text will be displayed as part of the list on the <em>Add vote type</em> page. This name must be unique.'),
      '#required' => TRUE,
      '#size' => 30,
    ];
    $form['id'] = [
      '#type' => 'machine_name',
      '#default_value' => $type
        ->id(),
      '#maxlength' => EntityTypeInterface::BUNDLE_MAX_LENGTH,
      '#machine_name' => [
        'exists' => [
          'Drupal\\votingapi\\Entity\\VoteType',
          'load',
        ],
        'source' => [
          'label',
        ],
      ],
      '#description' => $this
        ->t('A unique machine-readable name for this vote type. It must only contain lowercase letters, numbers, and underscores.', [
        '%vote-add' => $this
          ->t('Add vote type'),
      ]),
      '#disabled' => !$type
        ->isNew(),
    ];
    $form['value_type'] = [
      '#title' => $this
        ->t('Value type'),
      '#type' => 'textfield',
      '#default_value' => $type
        ->getValueType() ? $type
        ->getValueType() : 'points',
      '#description' => $this
        ->t('The type of value for this vote (percentage, points, etc.)'),
      '#required' => TRUE,
      '#size' => 30,
    ];
    $form['description'] = [
      '#title' => $this
        ->t('Description'),
      '#type' => 'textarea',
      '#default_value' => $type
        ->getDescription(),
      '#description' => $this
        ->t('Describe this vote type. The text will be displayed on administrative pages.'),
    ];
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  protected function actions(array $form, FormStateInterface $form_state) {
    $actions = parent::actions($form, $form_state);
    $actions['submit']['#value'] = $this
      ->t('Save vote type');
    $actions['delete']['#value'] = $this
      ->t('Delete vote type');
    return $actions;
  }

  /**
   * {@inheritdoc}
   */
  public function validateForm(array &$form, FormStateInterface $form_state) {
    parent::validateForm($form, $form_state);
    $id = trim($form_state
      ->getValue('type'));

    // '0' is invalid, since elsewhere we check it using empty().
    if ($id == '0') {
      $form_state
        ->setErrorByName('type', $this
        ->t("Invalid machine-readable name. Enter a name other than %invalid.", [
        '%invalid' => $id,
      ]));
    }
  }

  /**
   * {@inheritdoc}
   */
  public function save(array $form, FormStateInterface $form_state) {
    $type = $this->entity;
    $type
      ->set('id', trim($type
      ->id()));
    $type
      ->set('label', trim($type
      ->label()));
    $type
      ->set('value_type', trim($type
      ->getValueType()));
    $type
      ->set('description', trim($type
      ->getDescription()));
    $status = $type
      ->save();
    $t_args = [
      '%name' => $type
        ->label(),
    ];
    if ($status == SAVED_UPDATED) {
      $this
        ->messenger()
        ->addMessage($this
        ->t('The vote type %name has been updated.', $t_args));
    }
    elseif ($status == SAVED_NEW) {
      $this
        ->messenger()
        ->addMessage($this
        ->t('The vote type %name has been added.', $t_args));
      $context = array_merge($t_args, [
        'link' => $type
          ->toLink($this
          ->t('View'), 'collection')
          ->toString(),
      ]);
      $this
        ->logger('vote')
        ->notice('Added vote type %name.', $context);
    }
    $this->entityFieldManager
      ->clearCachedFieldDefinitions();
    $form_state
      ->setRedirectUrl($type
      ->toUrl('collection'));
  }

}

Classes

Namesort descending Description
VoteTypeForm Form controller for vote type forms.