3 namespace Drupal\diff\Plugin\views\field;
5 use Drupal\Core\Entity\RevisionableInterface;
6 use Drupal\Core\Form\FormStateInterface;
7 use Drupal\Core\Routing\RedirectDestinationTrait;
8 use Drupal\node\NodeInterface;
11 * Provides View field diff from plugin.
13 * @ViewsField("diff__from")
15 class DiffFrom extends DiffPluginBase {
17 use RedirectDestinationTrait;
22 protected function defineOptions() {
23 $options = parent::defineOptions();
25 $options['label']['default'] = t('From');
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);
39 * Returns the diff_to field ID.
42 * The diff_to field ID, or null if the field was not found on the view.
44 protected function getToFieldId() {
45 foreach ($this->view->field as $id => $field) {
46 if ($field instanceof DiffTo) {
53 * Submit handler for the bulk 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.
60 * @throws \Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException
61 * Thrown when the user tried to access an action without access to it.
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);
68 $diff_to = $form_state->getValue($this->getToFieldId());
69 $diff_to_entity = $this->loadEntityFromDiffFormKey($diff_to);
72 'query' => $this->getDestinationArray(),
74 $entity_type_id = $diff_from_entity->getEntityTypeId();
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(),
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(),