3 namespace Drupal\ctools\Wizard;
6 use Drupal\Core\DependencyInjection\ClassResolverInterface;
7 use Drupal\Core\Entity\EntityManagerInterface;
8 use Drupal\Core\Form\FormBuilderInterface;
9 use Drupal\Core\Form\FormStateInterface;
10 use Drupal\Core\Routing\RouteMatchInterface;
11 use Drupal\ctools\Event\WizardEvent;
12 use Drupal\user\SharedTempStoreFactory;
13 use Symfony\Component\EventDispatcher\EventDispatcherInterface;
16 * The base class for all entity form wizards.
18 abstract class EntityFormWizardBase extends FormWizardBase implements EntityFormWizardInterface {
23 * @var \Drupal\Core\Entity\EntityManagerInterface
25 protected $entityManager;
28 * @param \Drupal\user\SharedTempStoreFactory $tempstore
29 * Tempstore Factory for keeping track of values in each step of the
31 * @param \Drupal\Core\Form\FormBuilderInterface $builder
33 * @param \Drupal\Core\DependencyInjection\ClassResolverInterface $class_resolver
35 * @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $event_dispatcher
36 * The event dispatcher.
37 * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
39 * @param $tempstore_id
40 * The shared temp store factory collection name.
41 * @param null $machine_name
42 * The SharedTempStore key for our current wizard values.
44 * The current active step of the wizard.
46 public function __construct(SharedTempStoreFactory $tempstore, FormBuilderInterface $builder, ClassResolverInterface $class_resolver, EventDispatcherInterface $event_dispatcher, EntityManagerInterface $entity_manager, RouteMatchInterface $route_match, $tempstore_id, $machine_name = NULL, $step = NULL) {
47 $this->entityManager = $entity_manager;
48 parent::__construct($tempstore, $builder, $class_resolver, $event_dispatcher, $route_match, $tempstore_id, $machine_name, $step);
54 public static function getParameters() {
56 'tempstore' => \Drupal::service('user.shared_tempstore'),
57 'builder' => \Drupal::service('form_builder'),
58 'class_resolver' => \Drupal::service('class_resolver'),
59 'event_dispatcher' => \Drupal::service('event_dispatcher'),
60 'entity_manager' => \Drupal::service('entity.manager'),
67 public function initValues() {
68 $storage = $this->entityManager->getStorage($this->getEntityType());
69 if ($this->getMachineName()) {
70 $values = $this->getTempstore()->get($this->getMachineName());
72 $entity = $storage->load($this->getMachineName());
73 $values[$this->getEntityType()] = $entity;
74 $values['id'] = $entity->id();
75 $values['label'] = $entity->label();
79 $entity = $storage->create([]);
80 $values[$this->getEntityType()] = $entity;
82 $event = new WizardEvent($this, $values);
83 $this->dispatcher->dispatch(FormWizardInterface::LOAD_VALUES, $event);
84 return $event->getValues();
90 public function finish(array &$form, FormStateInterface $form_state) {
91 $cached_values = $form_state->getTemporaryValue('wizard');
92 /** @var $entity \Drupal\Core\Entity\EntityInterface */
93 $entity = $cached_values[$this->getEntityType()];
94 $entity->set('id', $cached_values['id']);
95 $entity->set('label', $cached_values['label']);
96 $status = $entity->save();
97 $definition = $this->entityManager->getDefinition($this->getEntityType());
99 drupal_set_message($this->t('Saved the %label @entity_type.', array(
100 '%label' => $entity->label(),
101 '@entity_type' => $definition->getLabel(),
105 drupal_set_message($this->t('The %label @entity_type was not saved.', array(
106 '%label' => $entity->label(),
107 '@entity_type' => $definition->getLabel(),
110 $form_state->setRedirectUrl($entity->toUrl('collection'));
111 parent::finish($form, $form_state);
115 * Helper function for generating label and id form elements.
118 * @param \Drupal\Core\Form\FormStateInterface $form_state
122 protected function customizeForm(array $form, FormStateInterface $form_state) {
123 $form = parent::customizeForm($form, $form_state);
124 if ($this->machine_name) {
125 $entity = $this->entityManager->getStorage($this->getEntityType())
126 ->load($this->machine_name);
131 $cached_values = $form_state->getTemporaryValue('wizard');
132 // If the entity already exists, allow for non-linear step interaction.
134 // Setup the step rendering theme element.
136 '#theme' => ['ctools_wizard_trail_links'],
138 '#cached_values' => $cached_values,
140 $form['#prefix'] = \Drupal::service('renderer')->render($prefix);
142 // Get the current form operation.
143 $operation = $this->getOperation($cached_values);
144 $operations = $this->getOperations($cached_values);
145 $default_operation = reset($operations);
146 if ($operation['form'] == $default_operation['form']) {
147 // Get the plugin definition of this entity.
148 $definition = $this->entityManager->getDefinition($this->getEntityType());
149 // Create id and label form elements.
150 $form['name'] = array(
151 '#type' => 'fieldset',
152 '#attributes' => array('class' => array('fieldset-no-legend')),
153 '#title' => $this->getWizardLabel(),
155 $form['name']['label'] = array(
156 '#type' => 'textfield',
157 '#title' => $this->getMachineLabel(),
160 '#default_value' => !empty($cached_values['label']) ? $cached_values['label'] : '',
162 '#disabled' => !empty($cached_values['label']),
164 $form['name']['id'] = array(
165 '#type' => 'machine_name',
167 '#machine_name' => array(
168 'source' => array('name', 'label'),
169 'exists' => $this->exists(),
171 '#description' => $this->t('A unique machine-readable name for this @entity_type. It must only contain lowercase letters, numbers, and underscores.', ['@entity_type' => $definition->getLabel()]),
172 '#default_value' => !empty($cached_values['id']) ? $cached_values['id'] : '',
173 '#disabled' => !empty($cached_values['id']),