ee3c3c0524db3145c0e645252316151d66c5304c
[yaffs-website] / web / modules / contrib / diff / src / Plugin / diff / Field / CommentFieldBuilder.php
1 <?php
2
3 namespace Drupal\diff\Plugin\diff\Field;
4
5 use Drupal\diff\FieldDiffBuilderBase;
6 use Drupal\Core\Field\FieldItemListInterface;
7 use Drupal\Core\Form\FormStateInterface;
8 use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface;
9
10 /**
11  * Plugin to diff comment fields.
12  *
13  * @FieldDiffBuilder(
14  *   id = "comment_field_diff_builder",
15  *   label = @Translation("Comment Field Diff"),
16  *   field_types = {
17  *     "comment"
18  *   },
19  * )
20  */
21 class CommentFieldBuilder extends FieldDiffBuilderBase {
22
23   /**
24    * {@inheritdoc}
25    */
26   public function build(FieldItemListInterface $field_items) {
27     $result = array();
28
29     // Every item from $field_items is of type FieldItemInterface.
30     foreach ($field_items as $field_key => $field_item) {
31       if (!$field_item->isEmpty()) {
32         $values = $field_item->getValue();
33         // Compare the key of the comment status.
34         if ($this->configuration['compare_key']) {
35           if (isset($values['status'])) {
36             $result[$field_key][] = $values['status'];
37           }
38         }
39         // A more human friendly representation.
40         if ($this->configuration['compare_string']) {
41           if (isset($values['status'])) {
42             switch ($values['status']) {
43               case CommentItemInterface::OPEN:
44                 $result[$field_key][] = $this->t('Comments for this entity are open.');
45                 break;
46
47               case CommentItemInterface::CLOSED:
48                 $result[$field_key][] = $this->t('Comments for this entity are closed.');
49                 break;
50
51               case CommentItemInterface::HIDDEN:
52                 $result[$field_key][] = $this->t('Comments for this entity are hidden.');
53                 break;
54             }
55           }
56         }
57       }
58     }
59
60     return $result;
61   }
62
63   /**
64    * {@inheritdoc}
65    */
66   public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
67     $form['compare_key'] = array(
68       '#type' => 'checkbox',
69       '#title' => $this->t('Compare comment status key'),
70       '#default_value' => $this->configuration['compare_key'],
71     );
72     $form['compare_string'] = array(
73       '#type' => 'checkbox',
74       '#title' => $this->t('Compare comment status string'),
75       '#default_value' => $this->configuration['compare_string'],
76     );
77
78     return parent::buildConfigurationForm($form, $form_state);
79   }
80
81   /**
82    * {@inheritdoc}
83    */
84   public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
85     $this->configuration['compare_key'] = $form_state->getValue('compare_key');
86     $this->configuration['compare_string'] = $form_state->getValue('compare_string');
87
88     parent::submitConfigurationForm($form, $form_state);
89   }
90
91   /**
92    * {@inheritdoc}
93    */
94   public function defaultConfiguration() {
95     $default_configuration = array(
96       'compare_key' => 0,
97       'compare_string' => 1,
98     );
99     $default_configuration += parent::defaultConfiguration();
100
101     return $default_configuration;
102   }
103
104 }