Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / web / modules / contrib / entity_browser / tests / modules / entity_browser_test / src / Form / FormElementTest.php
1 <?php
2
3 namespace Drupal\entity_browser_test\Form;
4
5 use Drupal\Core\Entity\EntityInterface;
6 use Drupal\Core\Form\FormBase;
7 use Drupal\Core\Form\FormStateInterface;
8 use Drupal\entity_browser\Element\EntityBrowserElement;
9
10 /**
11  * Provides a user login form.
12  */
13 class FormElementTest extends FormBase {
14
15   /**
16    * {@inheritdoc}
17    */
18   public function getFormId() {
19     return 'entity_browser_test_element';
20   }
21
22   /**
23    * {@inheritdoc}
24    */
25   public function buildForm(array $form, FormStateInterface $form_state) {
26     $form['#cache']['max-age'] = 0;
27
28     $form['fancy_entity_browser'] = [
29       '#type' => 'entity_browser',
30       '#entity_browser' => 'test_entity_browser_iframe',
31     ];
32
33     if ($default = \Drupal::request()->get('default_entity')) {
34       $form['fancy_entity_browser']['#default_value'] = [EntityBrowserElement::processEntityId($default)];
35     }
36
37     if ($selection_mode = \Drupal::request()->get('selection_mode')) {
38       $form['fancy_entity_browser']['#selection_mode'] = $selection_mode;
39     }
40
41     $form['main_submit'] = [
42       '#type' => 'submit',
43       '#value' => $this->t('Submit'),
44     ];
45
46     return $form;
47   }
48
49   /**
50    * {@inheritdoc}
51    */
52   public function submitForm(array &$form, FormStateInterface $form_state) {
53     $entities = $form_state->getValue(['fancy_entity_browser', 'entities']);
54
55     $message = 'Selected entities: ';
56     $message .= implode(', ', array_map(
57       function (EntityInterface $item) {
58         return $item->label();
59       },
60       $entities
61     ));
62     drupal_set_message($message);
63   }
64
65 }