UwEventEventSubscriber.php 1.51 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
<?php

namespace Drupal\uw_ct_event\EventSubscriber;

use Drupal\field_event_dispatcher\Event\Field\WidgetFormAlterEvent;
use Drupal\hook_event_dispatcher\HookEventDispatcherInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

/**
 * Class UwEventEventSubscriber.
 *
 * UW event event subscriber.
 *
 * @package Drupal\uw_ct_event\EventSubscriber
 */
class UwEventEventSubscriber implements EventSubscriberInterface {

  /**
   * Alter widget form.
   *
   * Remove 'This must be an external URL such as http://example.com' from the
   * link field.
   *
   * @param \Drupal\field_event_dispatcher\Event\Field\WidgetFormAlterEvent $event
   *   The event.
   */
  public function alterWidgetForm(WidgetFormAlterEvent $event): void {
    $element = &$event->getElement();
    // Only modify element 'Map' (field_uw_event_map).
    if (isset($element['#title']) && $element['#title'] == "Map") {
      $context = $event->getContext();
      // When creating or editing an event node,
      // remove 'This must be an external URL such as
      // http://example.com' from the link field.
      $forms = [
        'node-uw-ct-event-form',
        'node-uw-ct-event-edit-form',
      ];
      if (in_array($context['form']['#attributes']['class'][0], $forms)) {
        unset($element['uri']['#description']['#items'][1]);
      }
    }
  }

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    return [
      HookEventDispatcherInterface::WIDGET_FORM_ALTER => 'alterWidgetForm',
    ];
  }

}