8af0ecc0b009cd821bf981152953d7e7a2476047
[yaffs-website] / web / modules / contrib / diff / src / Plugin / views / field / DiffFrom.php
1 <?php
2
3 namespace Drupal\diff\Plugin\views\field;
4
5 use Drupal\Core\Entity\RevisionableInterface;
6 use Drupal\Core\Form\FormStateInterface;
7 use Drupal\Core\Routing\RedirectDestinationTrait;
8 use Drupal\node\NodeInterface;
9
10 /**
11  * Provides View field diff from plugin.
12  *
13  * @ViewsField("diff__from")
14  */
15 class DiffFrom extends DiffPluginBase {
16
17   use RedirectDestinationTrait;
18
19   /**
20    * {@inheritdoc}
21    */
22   protected function defineOptions() {
23     $options = parent::defineOptions();
24
25     $options['label']['default'] = t('From');
26     return $options;
27   }
28
29   /**
30    * {@inheritdoc}
31    */
32   public function viewsForm(array &$form, FormStateInterface $form_state) {
33     // Replace the form submit button label.
34     $form['actions']['submit']['#value'] = $this->t('Compare');
35     parent::viewsForm($form, $form_state);
36   }
37
38   /**
39    * Returns the diff_to field ID.
40    *
41    * @return string|null
42    *   The diff_to field ID, or null if the field was not found on the view.
43    */
44   protected function getToFieldId() {
45     foreach ($this->view->field as $id => $field) {
46       if ($field instanceof DiffTo) {
47         return $id;
48       }
49     }
50   }
51
52   /**
53    * Submit handler for the bulk form.
54    *
55    * @param array $form
56    *   An associative array containing the structure of the form.
57    * @param \Drupal\Core\Form\FormStateInterface $form_state
58    *   The current state of the form.
59    *
60    * @throws \Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException
61    *   Thrown when the user tried to access an action without access to it.
62    */
63   public function viewsFormSubmit(array &$form, FormStateInterface $form_state) {
64     if ($form_state->get('step') == 'views_form_views_form') {
65       $diff_from = $form_state->getValue($this->options['id']);
66       $diff_from_entity = $this->loadEntityFromDiffFormKey($diff_from);
67
68       $diff_to = $form_state->getValue($this->getToFieldId());
69       $diff_to_entity = $this->loadEntityFromDiffFormKey($diff_to);
70
71       $options = array(
72         'query' => $this->getDestinationArray(),
73       );
74       $entity_type_id = $diff_from_entity->getEntityTypeId();
75
76       $filter = \Drupal::service('plugin.manager.diff.layout')->getDefaultLayout();
77       if ($diff_from_entity instanceof NodeInterface && $diff_to_entity instanceof NodeInterface) {
78         $form_state->setRedirect('diff.revisions_diff', [
79           $entity_type_id => $diff_from_entity->id(),
80           'left_revision' => $diff_from_entity->getRevisionId(),
81           'right_revision' => $diff_to_entity->getRevisionId(),
82           'filter' => $filter,
83         ], $options);
84       }
85       elseif ($diff_from_entity instanceof RevisionableInterface && $diff_to_entity instanceof RevisionableInterface) {
86         $route_name = 'entity.' . $entity_type_id . '.revisions_diff';
87         $form_state->setRedirect($route_name, [
88           $entity_type_id => $diff_from_entity->id(),
89           'left_revision' => $diff_from_entity->getRevisionId(),
90           'right_revision' => $diff_to_entity->getRevisionId(),
91           'filter' => $filter,
92         ], $options);
93       }
94     }
95   }
96
97 }