Newer
Older
<?php
/**
* @file
* Contains \Drupal\entity\Form\EntityDisplayModeFormBase.
*/
namespace Drupal\entity\Form;
Alex Pott
committed
use Drupal\Core\Entity\EntityForm;
Angie Byron
committed
use Drupal\Core\Entity\EntityManagerInterface;
use Drupal\Core\Entity\Query\QueryFactory;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Provides the generic base class for entity display mode forms.
*/
Alex Pott
committed
abstract class EntityDisplayModeFormBase extends EntityForm {
/**
* The entity query factory.
*
* @var \Drupal\Core\Entity\Query\QueryFactory
*/
protected $queryFactory;
/**
* The entity type definition.
*
Alex Pott
committed
* @var \Drupal\Core\Entity\EntityTypeInterface
*/
protected $entityType;
Alex Pott
committed
/**
* The entity manager.
*
* @var \Drupal\Component\Plugin\PluginManagerInterface
*/
protected $entityManager;
/**
* Constructs a new EntityDisplayModeFormBase.
*
* @param \Drupal\Core\Entity\Query\QueryFactory $query_factory
* The entity query factory.
Angie Byron
committed
* @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
Alex Pott
committed
* The entity manager.
*/
Angie Byron
committed
public function __construct(QueryFactory $query_factory, EntityManagerInterface $entity_manager) {
$this->queryFactory = $query_factory;
Alex Pott
committed
$this->entityManager = $entity_manager;
}
/**
* {@inheritdoc}
*/
Alex Pott
committed
public static function create(ContainerInterface $container) {
return new static(
$container->get('entity.query'),
$container->get('entity.manager')
);
}
Alex Pott
committed
/**
* {@inheritdoc}
*/
protected function init(array &$form_state) {
parent::init($form_state);
$this->entityType = $this->entityManager->getDefinition($this->entity->getEntityTypeId());
Alex Pott
committed
}
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
/**
* {@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) {
// Do not allow to add internal 'default' view mode.
if ($entity_id == 'default') {
return TRUE;
}
return (bool) $this->queryFactory
->get($this->entity->getEntityTypeId())
->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' => $this->entityType->getLowercaseLabel())));
$this->entity->save();
\Drupal::entityManager()->clearCachedFieldDefinitions();
$form_state['redirect_route']['route_name'] = 'entity.' . $this->entity->getEntityTypeId() . '_list';