EntityDisplayModeFormBase.php in Zircon Profile 8
File
core/modules/field_ui/src/Form/EntityDisplayModeFormBase.php
View source
<?php
namespace Drupal\field_ui\Form;
use Drupal\Core\Entity\EntityForm;
use Drupal\Core\Entity\EntityManagerInterface;
use Drupal\Core\Entity\Query\QueryFactory;
use Drupal\Core\Form\FormStateInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
abstract class EntityDisplayModeFormBase extends EntityForm {
protected $queryFactory;
protected $entityType;
protected $entityManager;
public function __construct(QueryFactory $query_factory, EntityManagerInterface $entity_manager) {
$this->queryFactory = $query_factory;
$this->entityManager = $entity_manager;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('entity.query'), $container
->get('entity.manager'));
}
protected function init(FormStateInterface $form_state) {
parent::init($form_state);
$this->entityType = $this->entityManager
->getDefinition($this->entity
->getEntityTypeId());
}
public function form(array $form, FormStateInterface $form_state) {
$form['label'] = array(
'#type' => 'textfield',
'#title' => $this
->t('Name'),
'#maxlength' => 100,
'#default_value' => $this->entity
->label(),
);
$form['id'] = array(
'#type' => 'machine_name',
'#description' => $this
->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;
}
public function exists($entity_id, array $element) {
if ($entity_id == 'default') {
return TRUE;
}
return (bool) $this->queryFactory
->get($this->entity
->getEntityTypeId())
->condition('id', $element['#field_prefix'] . $entity_id)
->execute();
}
public function save(array $form, FormStateInterface $form_state) {
drupal_set_message($this
->t('Saved the %label @entity-type.', array(
'%label' => $this->entity
->label(),
'@entity-type' => $this->entityType
->getLowercaseLabel(),
)));
$this->entity
->save();
\Drupal::entityManager()
->clearCachedFieldDefinitions();
$form_state
->setRedirectUrl($this->entity
->urlInfo('collection'));
}
}