Version 1
[yaffs-website] / web / modules / contrib / diff / src / FieldDiffBuilderBase.php
1 <?php
2
3 namespace Drupal\diff;
4
5 use Drupal\Core\Field\FieldStorageDefinitionInterface;
6 use Drupal\Core\Form\FormStateInterface;
7 use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
8 use Drupal\Core\Plugin\PluginBase;
9 use Symfony\Component\DependencyInjection\ContainerInterface;
10 use Drupal\Core\Entity\EntityTypeManagerInterface;
11
12 /**
13  * Base class for field diff builder plugins.
14  */
15 abstract class FieldDiffBuilderBase extends PluginBase implements FieldDiffBuilderInterface, ContainerFactoryPluginInterface {
16
17   /**
18    * Contains the configuration object factory.
19    *
20    * @var \Drupal\Core\Config\ConfigFactoryInterface
21    */
22   protected $configFactory;
23
24   /**
25    * The entity type manager.
26    *
27    * @var \Drupal\Core\Entity\EntityTypeManagerInterface
28    */
29   protected $entityTypeManager;
30
31   /**
32    * The entity parser.
33    *
34    * @var \Drupal\diff\DiffEntityParser
35    */
36   protected $entityParser;
37
38   /**
39    * Constructs a FieldDiffBuilderBase object.
40    *
41    * @param array $configuration
42    *   A configuration array containing information about the plugin instance.
43    * @param string $plugin_id
44    *   The plugin_id for the plugin instance.
45    * @param mixed $plugin_definition
46    *   The plugin implementation definition.
47    * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
48    *   The entity type manager.
49    * @param \Drupal\diff\DiffEntityParser $entity_parser
50    *   The entity parser.
51    */
52   public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager, DiffEntityParser $entity_parser) {
53     $this->entityTypeManager = $entity_type_manager;
54     $this->entityParser = $entity_parser;
55     parent::__construct($configuration, $plugin_id, $plugin_definition);
56
57     $this->configuration += $this->defaultConfiguration();
58   }
59
60   /**
61    * {@inheritdoc}
62    */
63   public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
64     return new static(
65       $configuration,
66       $plugin_id,
67       $plugin_definition,
68       $container->get('entity_type.manager'),
69       $container->get('diff.entity_parser')
70     );
71   }
72
73   /**
74    * {@inheritdoc}
75    */
76   public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
77     $form['show_header'] = array(
78       '#type' => 'checkbox',
79       '#title' => $this->t('Show field title'),
80       '#weight' => -5,
81       '#default_value' => $this->configuration['show_header'],
82     );
83     $form['markdown'] = array(
84       '#type' => 'select',
85       '#title' => $this->t('Markdown callback'),
86       '#default_value' => $this->configuration['markdown'],
87       '#options' => array(
88         'drupal_html_to_text' => $this->t('Drupal HTML to Text'),
89         'filter_xss' => $this->t('Filter XSS (some tags)'),
90         'filter_xss_all' => $this->t('Filter XSS (all tags)'),
91       ),
92       '#description' => $this->t('These provide ways to clean markup tags to make comparisons easier to read.'),
93     );
94
95     return $form;
96   }
97
98   /**
99    * {@inheritdoc}
100    */
101   public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
102     // By default an empty validation function is provided.
103   }
104
105   /**
106    * {@inheritdoc}
107    */
108   public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
109     $this->configuration['show_header'] = $form_state->getValue('show_header');
110     $this->configuration['markdown'] = $form_state->getValue('markdown');
111   }
112
113   /**
114    * {@inheritdoc}
115    */
116   public function defaultConfiguration() {
117     return array(
118       'show_header' => 1,
119       'markdown' => 'drupal_html_to_text',
120     );
121   }
122
123   /**
124    * {@inheritdoc}
125    */
126   public function getConfiguration() {
127     return $this->configuration;
128   }
129
130   /**
131    * {@inheritdoc}
132    */
133   public function setConfiguration(array $configuration) {
134     $this->configuration = $configuration + $this->defaultConfiguration();
135   }
136
137   /**
138    * {@inheritdoc}
139    */
140   public function calculateDependencies() {
141     return array();
142   }
143
144   /**
145    * {@inheritdoc}
146    */
147   public static function isApplicable(FieldStorageDefinitionInterface $field_definition) {
148     return TRUE;
149   }
150
151 }