3 namespace Drupal\diff\Plugin\diff\Field;
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;
11 * Plugin to diff comment fields.
14 * id = "comment_field_diff_builder",
15 * label = @Translation("Comment Field Diff"),
21 class CommentFieldBuilder extends FieldDiffBuilderBase {
26 public function build(FieldItemListInterface $field_items) {
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'];
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.');
47 case CommentItemInterface::CLOSED:
48 $result[$field_key][] = $this->t('Comments for this entity are closed.');
51 case CommentItemInterface::HIDDEN:
52 $result[$field_key][] = $this->t('Comments for this entity are hidden.');
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'],
72 $form['compare_string'] = array(
73 '#type' => 'checkbox',
74 '#title' => $this->t('Compare comment status string'),
75 '#default_value' => $this->configuration['compare_string'],
78 return parent::buildConfigurationForm($form, $form_state);
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');
88 parent::submitConfigurationForm($form, $form_state);
94 public function defaultConfiguration() {
95 $default_configuration = array(
97 'compare_string' => 1,
99 $default_configuration += parent::defaultConfiguration();
101 return $default_configuration;