Updated all the contrib modules to their latest versions.
[yaffs-website] / web / modules / contrib / entity_browser / tests / modules / entity_browser_test / src / Plugin / EntityBrowser / Widget / DummyWidget.php
1 <?php
2
3 namespace Drupal\entity_browser_test\Plugin\EntityBrowser\Widget;
4
5 use Drupal\Core\Access\AccessResult;
6 use Drupal\Core\Form\FormStateInterface;
7 use Drupal\entity_browser\WidgetBase;
8
9 /**
10  * Dummy widget implementation for test purposes.
11  *
12  * @EntityBrowserWidget(
13  *   id = "dummy",
14  *   label = @Translation("Dummy widget"),
15  *   description = @Translation("Dummy widget existing for testing purposes."),
16  *   auto_select = FALSE
17  * )
18  */
19 class DummyWidget extends WidgetBase {
20
21   /**
22    * Entity to be returned.
23    *
24    * @var \Drupal\Core\Entity\EntityInterface
25    */
26   public $entity;
27
28   /**
29    * {@inheritdoc}
30    */
31   public function defaultConfiguration() {
32     return ['text' => ''] + parent::defaultConfiguration();
33   }
34
35   /**
36    * {@inheritdoc}
37    */
38   public function getForm(array &$original_form, FormStateInterface $form_state, array $additional_widget_parameters) {
39     return [
40       '#markup' => $this->configuration['text'],
41       '#parents' => [],
42     ];
43   }
44
45   /**
46    * {@inheritdoc}
47    */
48   public function submit(array &$element, array &$form, FormStateInterface $form_state) {
49     $this->selectEntities([$this->entity], $form_state);
50   }
51
52   /**
53    * {@inheritdoc}
54    */
55   protected function prepareEntities(array $form, FormStateInterface $form_state) {
56     return $form_state->getValue('dummy_entities', []);
57   }
58
59   /**
60    * {@inheritdoc}
61    */
62   public function access() {
63     if (\Drupal::state()->get('eb_test_dummy_widget_access', TRUE)) {
64       $access = AccessResult::allowed();
65       $access->addCacheContexts(['eb_dummy']);
66     }
67     else {
68       $access = AccessResult::forbidden();
69       $access->addCacheContexts(['eb_dummy']);
70     }
71     return $access;
72   }
73
74 }