EntityDisplayModeFormBase.php 3.37 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
<?php

/**
 * @file
 * Contains \Drupal\entity\Form\EntityDisplayModeFormBase.
 */

namespace Drupal\entity\Form;

use Drupal\Core\Entity\EntityFormController;
11
use Drupal\Core\Entity\EntityManager;
12 13 14 15 16 17
use Drupal\Core\Entity\Query\QueryFactory;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Provides the generic base class for entity display mode forms.
 */
18
abstract class EntityDisplayModeFormBase extends EntityFormController {
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33

  /**
   * The entity query factory.
   *
   * @var \Drupal\Core\Entity\Query\QueryFactory
   */
  protected $queryFactory;

  /**
   * The entity type definition.
   *
   * @var array
   */
  protected $entityInfo;

34 35 36 37 38 39 40
  /**
   * The entity manager.
   *
   * @var \Drupal\Component\Plugin\PluginManagerInterface
   */
  protected $entityManager;

41 42 43 44 45
  /**
   * Constructs a new EntityDisplayModeFormBase.
   *
   * @param \Drupal\Core\Entity\Query\QueryFactory $query_factory
   *   The entity query factory.
46 47
   * @param \Drupal\Core\Entity\EntityManager $entity_manager
   *   The entity manager.
48
   */
49
  public function __construct(QueryFactory $query_factory, EntityManager $entity_manager) {
50
    $this->queryFactory = $query_factory;
51
    $this->entityManager = $entity_manager;
52 53 54 55 56
  }

  /**
   * {@inheritdoc}
   */
57
  public static function create(ContainerInterface $container) {
58 59
    return new static(
      $container->get('entity.query'),
60
      $container->get('entity.manager')
61 62 63
    );
  }

64 65 66 67 68 69 70 71
  /**
   * {@inheritdoc}
   */
  protected function init(array &$form_state) {
    parent::init($form_state);
    $this->entityInfo = $this->entityManager->getDefinition($this->entity->entityType());
  }

72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
  /**
   * {@inheritdoc}
   */
  public function form(array $form, array &$form_state) {
    $form['label'] = array(
      '#type' => 'textfield',
      '#title' => t('Label'),
      '#maxlength' => 100,
      '#default_value' => $this->entity->label(),
    );

    $form['id'] = array(
      '#type' => 'machine_name',
      '#description' => t('A unique machine-readable name. Can only contain lowercase letters, numbers, and underscores.'),
      '#disabled' => !$this->entity->isNew(),
      '#default_value' => $this->entity->id(),
      '#field_prefix' => $this->entity->isNew() ? $this->entity->getTargetType() . '.' : '',
      '#machine_name' => array(
        'exists' => array($this, 'exists'),
        'replace_pattern' => '[^a-z0-9_.]+',
      ),
    );

    return $form;
  }

  /**
   * Determines if the display mode already exists.
   *
   * @param string|int $entity_id
   *   The entity ID.
   * @param array $element
   *   The form element.
   * @param array $form_state
   *   The form state.
   *
   * @return bool
   *   TRUE if the display mode exists, FALSE otherwise.
   */
  public function exists($entity_id, array $element, array $form_state) {
    return (bool) $this->queryFactory
      ->get($this->entity->entityType())
      ->condition('id', $element['#field_prefix'] . $entity_id)
      ->execute();
  }

  /**
   * {@inheritdoc}
   */
  public function save(array $form, array &$form_state) {
    drupal_set_message(t('Saved the %label @entity-type.', array('%label' => $this->entity->label(), '@entity-type' => strtolower($this->entityInfo['label']))));
    $this->entity->save();
    entity_info_cache_clear();
    $short_type = str_replace('_mode', '', $this->entity->entityType());
    $form_state['redirect'] = "admin/structure/display-modes/$short_type";
  }

}