Version 1
[yaffs-website] / web / core / modules / system / tests / modules / condition_test / src / FormController.php
1 <?php
2
3 namespace Drupal\condition_test;
4
5 use Drupal\Core\Form\FormInterface;
6 use Drupal\Core\Condition\ConditionManager;
7 use Drupal\Core\Form\FormStateInterface;
8 use Drupal\node\Entity\Node;
9
10 /**
11  * Routing controller class for condition_test testing of condition forms.
12  */
13 class FormController implements FormInterface {
14
15   /**
16    * The condition plugin we will be working with.
17    *
18    * @var \Drupal\Core\Condition\ConditionInterface
19    */
20   protected $condition;
21
22   /**
23    * {@inheritdoc}
24    */
25   public function getFormId() {
26     return 'condition_node_type_test_form';
27   }
28
29   /**
30    * Constructs a \Drupal\condition_test\FormController object.
31    */
32   public function __construct() {
33     $manager = new ConditionManager(\Drupal::service('container.namespaces'), \Drupal::cache('discovery'), \Drupal::moduleHandler());
34     $this->condition = $manager->createInstance('node_type');
35   }
36
37   /**
38    * {@inheritdoc}
39    */
40   public function buildForm(array $form, FormStateInterface $form_state) {
41     $form = $this->condition->buildConfigurationForm($form, $form_state);
42     $form['actions']['submit'] = [
43       '#type' => 'submit',
44       '#value' => t('Submit'),
45     ];
46     return $form;
47   }
48
49   /**
50    * Implements \Drupal\Core\Form\FormInterface::validateForm().
51    */
52   public function validateForm(array &$form, FormStateInterface $form_state) {
53     $this->condition->validateConfigurationForm($form, $form_state);
54   }
55
56   /**
57    * {@inheritdoc}
58    */
59   public function submitForm(array &$form, FormStateInterface $form_state) {
60     $this->condition->submitConfigurationForm($form, $form_state);
61     $config = $this->condition->getConfig();
62     foreach ($config['bundles'] as $bundle) {
63       drupal_set_message('Bundle: ' . $bundle);
64     }
65
66     $article = Node::load(1);
67     $this->condition->setContextValue('node', $article);
68     if ($this->condition->execute()) {
69       drupal_set_message(t('Executed successfully.'));
70     }
71   }
72
73 }