1647f734aca55aea4690bb0c3f24c0da6de922ee
[yaffs-website] / web / modules / contrib / ctools / src / Wizard / EntityFormWizardBase.php
1 <?php
2
3 namespace Drupal\ctools\Wizard;
4
5
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;
14
15 /**
16  * The base class for all entity form wizards.
17  */
18 abstract class EntityFormWizardBase extends FormWizardBase implements EntityFormWizardInterface {
19
20   /**
21    * The entity manager.
22    *
23    * @var \Drupal\Core\Entity\EntityManagerInterface
24    */
25   protected $entityManager;
26
27   /**
28    * @param \Drupal\user\SharedTempStoreFactory $tempstore
29    *   Tempstore Factory for keeping track of values in each step of the
30    *   wizard.
31    * @param \Drupal\Core\Form\FormBuilderInterface $builder
32    *   The Form Builder.
33    * @param \Drupal\Core\DependencyInjection\ClassResolverInterface $class_resolver
34    *   The class resolver.
35    * @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $event_dispatcher
36    *   The event dispatcher.
37    * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
38    *   The 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.
43    * @param null $step
44    *   The current active step of the wizard.
45    */
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);
49   }
50
51   /**
52    * {@inheritdoc}
53    */
54   public static function getParameters() {
55     return [
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'),
61     ];
62   }
63
64   /**
65    * {@inheritdoc}
66    */
67   public function initValues() {
68     $storage = $this->entityManager->getStorage($this->getEntityType());
69     if ($this->getMachineName()) {
70       $values = $this->getTempstore()->get($this->getMachineName());
71       if (!$values) {
72         $entity = $storage->load($this->getMachineName());
73         $values[$this->getEntityType()] = $entity;
74         $values['id'] = $entity->id();
75         $values['label'] = $entity->label();
76       }
77     }
78     else {
79       $entity = $storage->create([]);
80       $values[$this->getEntityType()] = $entity;
81     }
82     $event = new WizardEvent($this, $values);
83     $this->dispatcher->dispatch(FormWizardInterface::LOAD_VALUES, $event);
84     return $event->getValues();
85   }
86
87   /**
88    * {@inheritdoc}
89    */
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());
98     if ($status) {
99       drupal_set_message($this->t('Saved the %label @entity_type.', array(
100         '%label' => $entity->label(),
101         '@entity_type' => $definition->getLabel(),
102       )));
103     }
104     else {
105       drupal_set_message($this->t('The %label @entity_type was not saved.', array(
106         '%label' => $entity->label(),
107         '@entity_type' => $definition->getLabel(),
108       )));
109     }
110     $form_state->setRedirectUrl($entity->toUrl('collection'));
111     parent::finish($form, $form_state);
112   }
113
114   /**
115    * Helper function for generating label and id form elements.
116    *
117    * @param array $form
118    * @param \Drupal\Core\Form\FormStateInterface $form_state
119    *
120    * @return array
121    */
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);
127     }
128     else {
129       $entity = NULL;
130     }
131     $cached_values = $form_state->getTemporaryValue('wizard');
132     // If the entity already exists, allow for non-linear step interaction.
133     if ($entity) {
134       // Setup the step rendering theme element.
135       $prefix = [
136         '#theme' => ['ctools_wizard_trail_links'],
137         '#wizard' => $this,
138         '#cached_values' => $cached_values,
139       ];
140       $form['#prefix'] = \Drupal::service('renderer')->render($prefix);
141     }
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(),
154       );
155       $form['name']['label'] = array(
156         '#type' => 'textfield',
157         '#title' => $this->getMachineLabel(),
158         '#required' => TRUE,
159         '#size' => 32,
160         '#default_value' => !empty($cached_values['label']) ? $cached_values['label'] : '',
161         '#maxlength' => 255,
162         '#disabled' => !empty($cached_values['label']),
163       );
164       $form['name']['id'] = array(
165         '#type' => 'machine_name',
166         '#maxlength' => 128,
167         '#machine_name' => array(
168           'source' => array('name', 'label'),
169           'exists' => $this->exists(),
170         ),
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']),
174       );
175     }
176
177     return $form;
178   }
179
180 }