Further Drupal 8.6.4 changes. Some core files were not committed before a commit...
[yaffs-website] / web / core / modules / comment / src / Plugin / Field / FieldWidget / CommentWidget.php
1 <?php
2
3 namespace Drupal\comment\Plugin\Field\FieldWidget;
4
5 use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface;
6 use Drupal\Component\Utility\Html;
7 use Drupal\Core\Field\FieldItemListInterface;
8 use Drupal\Core\Field\WidgetBase;
9 use Drupal\Core\Form\FormStateInterface;
10
11 /**
12  * Provides a default comment widget.
13  *
14  * @FieldWidget(
15  *   id = "comment_default",
16  *   label = @Translation("Comment"),
17  *   field_types = {
18  *     "comment"
19  *   }
20  * )
21  */
22 class CommentWidget extends WidgetBase {
23
24   /**
25    * {@inheritdoc}
26    */
27   public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
28     $entity = $items->getEntity();
29
30     $element['status'] = [
31       '#type' => 'radios',
32       '#title' => t('Comments'),
33       '#title_display' => 'invisible',
34       '#default_value' => $items->status,
35       '#options' => [
36         CommentItemInterface::OPEN => t('Open'),
37         CommentItemInterface::CLOSED => t('Closed'),
38         CommentItemInterface::HIDDEN => t('Hidden'),
39       ],
40       CommentItemInterface::OPEN => [
41         '#description' => t('Users with the "Post comments" permission can post comments.'),
42       ],
43       CommentItemInterface::CLOSED => [
44         '#description' => t('Users cannot post comments, but existing comments will be displayed.'),
45       ],
46       CommentItemInterface::HIDDEN => [
47         '#description' => t('Comments are hidden from view.'),
48       ],
49     ];
50     // If the entity doesn't have any comments, the "hidden" option makes no
51     // sense, so don't even bother presenting it to the user unless this is the
52     // default value widget on the field settings form.
53     if (!$this->isDefaultValueWidget($form_state) && !$items->comment_count) {
54       $element['status'][CommentItemInterface::HIDDEN]['#access'] = FALSE;
55       // Also adjust the description of the "closed" option.
56       $element['status'][CommentItemInterface::CLOSED]['#description'] = t('Users cannot post comments.');
57     }
58     // If the advanced settings tabs-set is available (normally rendered in the
59     // second column on wide-resolutions), place the field as a details element
60     // in this tab-set.
61     if (isset($form['advanced'])) {
62       // Get default value from the field.
63       $field_default_values = $this->fieldDefinition->getDefaultValue($entity);
64
65       // Override widget title to be helpful for end users.
66       $element['#title'] = $this->t('Comment settings');
67
68       $element += [
69         '#type' => 'details',
70         // Open the details when the selected value is different to the stored
71         // default values for the field.
72         '#open' => ($items->status != $field_default_values[0]['status']),
73         '#group' => 'advanced',
74         '#attributes' => [
75           'class' => ['comment-' . Html::getClass($entity->getEntityTypeId()) . '-settings-form'],
76         ],
77         '#attached' => [
78           'library' => ['comment/drupal.comment'],
79         ],
80       ];
81     }
82
83     return $element;
84   }
85
86   /**
87    * {@inheritdoc}
88    */
89   public function massageFormValues(array $values, array $form, FormStateInterface $form_state) {
90     // Add default values for statistics properties because we don't want to
91     // have them in form.
92     foreach ($values as &$value) {
93       $value += [
94         'cid' => 0,
95         'last_comment_timestamp' => 0,
96         'last_comment_name' => '',
97         'last_comment_uid' => 0,
98         'comment_count' => 0,
99       ];
100     }
101     return $values;
102   }
103
104 }