diff --git a/core/lib/Drupal/Core/Field/Plugin/Field/FieldWidget/EmailDefaultWidget.php b/core/lib/Drupal/Core/Field/Plugin/Field/FieldWidget/EmailDefaultWidget.php index 31bb908..c82257f 100644 --- a/core/lib/Drupal/Core/Field/Plugin/Field/FieldWidget/EmailDefaultWidget.php +++ b/core/lib/Drupal/Core/Field/Plugin/Field/FieldWidget/EmailDefaultWidget.php @@ -10,6 +10,7 @@ use Drupal\Core\Field\FieldItemListInterface; use Drupal\Core\Field\WidgetBase; use Drupal\Core\Form\FormStateInterface; +use Drupal\Core\Render\Element\Email; /** * Plugin implementation of the 'email_default' widget. @@ -73,7 +74,7 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen '#default_value' => isset($items[$delta]->value) ? $items[$delta]->value : NULL, '#placeholder' => $this->getSetting('placeholder'), '#size' => $this->getSetting('size'), - '#maxlength' => EMAIL_MAX_LENGTH, + '#maxlength' => Email::EMAIL_MAX_LENGTH, ); return $element; } diff --git a/core/modules/comment/comment.info.yml b/core/modules/comment/comment.info.yml index c841981..d81583e 100644 --- a/core/modules/comment/comment.info.yml +++ b/core/modules/comment/comment.info.yml @@ -5,6 +5,6 @@ package: Core version: VERSION core: 8.x dependencies: - - datetime + - entity_reference - text configure: comment.admin diff --git a/core/modules/comment/src/CommentAccessControlHandler.php b/core/modules/comment/src/CommentAccessControlHandler.php index 77367ab..3b62b92 100644 --- a/core/modules/comment/src/CommentAccessControlHandler.php +++ b/core/modules/comment/src/CommentAccessControlHandler.php @@ -9,6 +9,8 @@ use Drupal\Core\Entity\EntityAccessControlHandler; use Drupal\Core\Entity\EntityInterface; +use Drupal\Core\Field\FieldDefinitionInterface; +use Drupal\Core\Field\FieldItemListInterface; use Drupal\Core\Session\AccountInterface; /** @@ -51,4 +53,32 @@ protected function checkCreateAccess(AccountInterface $account, array $context, return $account->hasPermission('post comments'); } + /** + * {@inheritdoc} + */ + protected function checkFieldAccess($operation, FieldDefinitionInterface $field_definition, AccountInterface $account, FieldItemListInterface $items = NULL) { + if ($operation == 'edit') { + $comment = $items->getEntity(); + $is_admin_edit = $items->getEntity()->id() && $account->hasPermission('administer comments'); + $admin_fields = array('created', 'uid', 'status'); + if (in_array($field_definition->getName(), $admin_fields)) { + var_dump($is_admin_edit, $field_definition->getName()); + return $is_admin_edit; + } + + if (in_array($field_definition->getName(), array('homepage', 'mail', 'name'))) { + /** @var \Drupal\comment\CommentInterface $comment */ + $entity = \Drupal::entityManager()->getStorage($comment->getCommentedEntityTypeId())->load($comment->getCommentedEntityId()); + $comment_field_definition = \Drupal::entityManager()->getFieldDefinitions($entity->getEntityTypeId(), $entity->bundle())[$comment->getFieldName()]; + $anonymous_contact = $comment_field_definition->getSetting('anonymous'); + var_dump($is_admin_edit || ($account->isAnonymous() && $anonymous_contact != COMMENT_ANONYMOUS_MAYNOT_CONTACT), $field_definition->getName()); + return $is_admin_edit || ($account->isAnonymous() && $anonymous_contact != COMMENT_ANONYMOUS_MAYNOT_CONTACT); + } + + } + + return parent::checkFieldAccess($operation, $field_definition, $account, $items); + } + + } diff --git a/core/modules/comment/src/CommentForm.php b/core/modules/comment/src/CommentForm.php index 8ae6c2d..8c4e0c9 100644 --- a/core/modules/comment/src/CommentForm.php +++ b/core/modules/comment/src/CommentForm.php @@ -8,14 +8,13 @@ namespace Drupal\comment; use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface; +use Drupal\Component\Utility\Html; use Drupal\Component\Utility\String; use Drupal\Component\Utility\Unicode; -use Drupal\Core\Datetime\DrupalDateTime; use Drupal\Core\Entity\ContentEntityForm; use Drupal\Core\Entity\EntityManagerInterface; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Language\Language; -use Drupal\Core\Language\LanguageInterface; use Drupal\Core\Session\AccountInterface; use Symfony\Component\DependencyInjection\ContainerInterface; @@ -57,22 +56,6 @@ public function __construct(EntityManagerInterface $entity_manager, AccountInter /** * {@inheritdoc} */ - protected function init(FormStateInterface $form_state) { - $comment = $this->entity; - - // Make the comment inherit the current content language unless specifically - // set. - if ($comment->isNew()) { - $language_content = \Drupal::languageManager()->getCurrentLanguage(LanguageInterface::TYPE_CONTENT); - $comment->langcode->value = $language_content->id; - } - - parent::init($form_state); - } - - /** - * Overrides Drupal\Core\Entity\EntityForm::form(). - */ public function form(array $form, FormStateInterface $form_state) { /** @var \Drupal\comment\CommentInterface $comment */ $comment = $this->entity; @@ -83,7 +66,7 @@ public function form(array $form, FormStateInterface $form_state) { $field_definition = $this->entityManager->getFieldDefinitions($entity->getEntityTypeId(), $entity->bundle())[$comment->getFieldName()]; // Use #comment-form as unique jump target, regardless of entity type. - $form['#id'] = drupal_html_id('comment_form'); + $form['#id'] = Html::getUniqueId('comment_form'); $form['#theme'] = array('comment_form__' . $entity->getEntityTypeId() . '__' . $entity->bundle() . '__' . $field_name, 'comment_form'); $anonymous_contact = $field_definition->getSetting('anonymous'); @@ -126,32 +109,13 @@ public function form(array $form, FormStateInterface $form_state) { $status = ($this->currentUser->hasPermission('skip comment approval') ? CommentInterface::PUBLISHED : CommentInterface::NOT_PUBLISHED); } - $date = ''; - if ($comment->id()) { - $date = !empty($comment->date) ? $comment->date : DrupalDateTime::createFromTimestamp($comment->getCreatedTime()); - } - - $form['uid'] = array( - '#type' => 'value', - '#value' => $comment->getOwnerId(), - ); - // Add the author name field depending on the current user. - $form['name']['widget'][0]['value']['#required'] = ($this->currentUser->isAnonymous() && $anonymous_contact == COMMENT_ANONYMOUS_MUST_CONTACT); - if ($is_admin) { - $form['name']['#group'] = 'author'; - $form['name']['widget'][0]['value']['#title'] = $this->t('Authored by'); - $form['name']['widget'][0]['value']['#description'] = $this->t('Leave blank for %anonymous.', array('%anonymous' => $this->config('user.settings')->get('anonymous'))); - $form['name']['widget'][0]['value']['#autocomplete_route_name'] = 'user.autocomplete'; - } - elseif ($this->currentUser->isAuthenticated()) { - $form['name']['widget'][0]['value']['#type'] = 'item'; - $form['name']['widget'][0]['value']['#value'] = $this->currentUser->getUsername(); - $form['name']['widget'][0]['value']['#theme'] = 'username'; - $form['name']['widget'][0]['value']['#account'] = $this->currentUser; - } - elseif($this->currentUser->isAnonymous()) { - $form['author']['name']['#attributes']['data-drupal-default-value'] = $this->config('user.settings')->get('anonymous'); + $form['uid']['widget'][0]['value']['#required'] = ($this->currentUser->isAnonymous() && $anonymous_contact == COMMENT_ANONYMOUS_MUST_CONTACT); + if ($this->currentUser->isAuthenticated()) { + $form['uid']['widget'][0]['value']['#type'] = 'item'; + $form['uid']['widget'][0]['value']['#value'] = $this->currentUser->id(); + $form['uid']['widget'][0]['value']['#theme'] = 'username'; + $form['uid']['widget'][0]['value']['#account'] = $this->currentUser; } $language_configuration = \Drupal::moduleHandler()->invoke('language', 'get_default_configuration', array('comment', $comment->getTypeId())); @@ -164,26 +128,12 @@ public function form(array $form, FormStateInterface $form_state) { ); // Add author email and homepage fields depending on the current user. - // @todo Leverage field access https://www.drupal.org/node/2098419 $form['mail']['widget'][0]['value']['#required'] = ($this->currentUser->isAnonymous() && $anonymous_contact == COMMENT_ANONYMOUS_MUST_CONTACT); - $form['mail']['widget'][0]['value']['#access'] = $is_admin || ($this->currentUser->isAnonymous() && $anonymous_contact != COMMENT_ANONYMOUS_MAYNOT_CONTACT); - if ($is_admin) { - $form['mail']['#group'] = 'author'; - } - $form['homepage']['widget'][0]['value']['#access'] = $is_admin || ($this->currentUser->isAnonymous() && $anonymous_contact != COMMENT_ANONYMOUS_MAYNOT_CONTACT); - if ($is_admin) { - $form['homepage']['#group'] = 'author'; - } - - // Add administrative comment publishing options. - $form['author']['date'] = array( - '#type' => 'datetime', - '#title' => $this->t('Authored on'), - '#default_value' => $date, - '#size' => 20, - '#access' => $is_admin, - ); + $form['uid']['#group'] = 'author'; + $form['mail']['#group'] = 'author'; + $form['homepage']['#group'] = 'author'; + $form['created']['#group'] = 'author'; $form['author']['status'] = array( '#type' => 'radios', @@ -193,20 +143,14 @@ public function form(array $form, FormStateInterface $form_state) { CommentInterface::PUBLISHED => $this->t('Published'), CommentInterface::NOT_PUBLISHED => $this->t('Not published'), ), - '#access' => $is_admin, - ); - - // Used for conditional validation of author fields. - $form['is_anonymous'] = array( - '#type' => 'value', - '#value' => ($comment->id() ? !$comment->getOwnerId() : $this->currentUser->isAnonymous()), + '#access' => $comment->status->access('edit'), ); return $form; } /** - * Overrides Drupal\Core\Entity\EntityForm::actions(). + * {@inheritdoc} */ protected function actions(array $form, FormStateInterface $form_state) { $element = parent::actions($form, $form_state); @@ -238,56 +182,7 @@ protected function actions(array $form, FormStateInterface $form_state) { } /** - * Overrides Drupal\Core\Entity\EntityForm::validate(). - */ - public function validate(array $form, FormStateInterface $form_state) { - parent::validate($form, $form_state); - $entity = $this->entity; - - if (!$entity->isNew()) { - // Verify the name in case it is being changed from being anonymous. - $accounts = $this->entityManager->getStorage('user')->loadByProperties(array('name' => $form_state->getValue('name'))); - $account = reset($accounts); - $form_state->setValue('uid', $account ? $account->id() : 0); - - $date = $form_state->getValue('date'); - if ($date instanceOf DrupalDateTime && $date->hasErrors()) { - $form_state->setErrorByName('date', $this->t('You have to specify a valid date.')); - } - if ($form_state['values']['name'][0]['value'] && !$form_state->getValue('is_anonymous') && !$account) { - $form_state->setErrorByName('name][0][value', $this->t('You have to specify a valid author.')); - } - } - elseif ($form_state->getValue('is_anonymous')) { - // Validate anonymous comment author fields (if given). If the (original) - // author of this comment was an anonymous user, verify that no registered - // user with this name exists. - if ($form_state['values']['name'][0]['value']) { - $accounts = $this->entityManager->getStorage('user')->loadByProperties(array('name' => $form_state->getValue(array('name', 0, 'value')))); - if (!empty($accounts)) { - $form_state->setErrorByName('name][0][value', $this->t('The name you used belongs to a registered user.')); - } - } - } - } - - /** - * Overrides EntityForm::buildEntity(). - */ - public function buildEntity(array $form, FormStateInterface $form_state) { - $comment = parent::buildEntity($form, $form_state); - if (!$form_state->isValueEmpty('date') && $form_state->getValue('date') instanceOf DrupalDateTime) { - $comment->setCreatedTime($form_state->getValue('date')->getTimestamp()); - } - else { - $comment->setCreatedTime(REQUEST_TIME); - } - $comment->changed->value = REQUEST_TIME; - return $comment; - } - - /** - * Overrides Drupal\Core\Entity\EntityForm::submit(). + * {@inheritdoc} */ public function submit(array $form, FormStateInterface $form_state) { /** @var \Drupal\comment\CommentInterface $comment */ @@ -297,9 +192,6 @@ public function submit(array $form, FormStateInterface $form_state) { // @todo Too fragile. Should be prepared and stored in comment_form() // already. $author_name = $comment->getAuthorName(); - if (!$comment->is_anonymous && !empty($author_name) && ($account = user_load_by_name($author_name))) { - $comment->setOwner($account); - } // If the comment was posted by an anonymous user and no author name was // required, use "Anonymous" by default. if ($comment->is_anonymous && (!isset($author_name) || $author_name === '')) { @@ -341,7 +233,7 @@ public function preview(array &$form, FormStateInterface $form_state) { } /** - * Overrides Drupal\Core\Entity\EntityForm::save(). + * {@inheritdoc} */ public function save(array $form, FormStateInterface $form_state) { $comment = $this->entity; diff --git a/core/modules/comment/src/Entity/Comment.php b/core/modules/comment/src/Entity/Comment.php index 6ec8d92..94e50f4 100644 --- a/core/modules/comment/src/Entity/Comment.php +++ b/core/modules/comment/src/Entity/Comment.php @@ -241,10 +241,23 @@ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) { ->setDescription(t('The user ID of the comment author.')) ->setTranslatable(TRUE) ->setSetting('target_type', 'user') - ->setDefaultValue(0); + ->setSetting('handler', 'default') + ->setDefaultValueCallback(array('Drupal\comment\Entity\Comment', 'getCurrentUserId')) + ->setDisplayOptions('form', array( + 'type' => 'entity_reference_autocomplete', + 'weight' => 5, + 'settings' => array( + 'match_operator' => 'CONTAINS', + 'size' => '60', + 'autocomplete_type' => 'tags', + 'placeholder' => '', + ), + )) + ->setDisplayConfigurable('form', TRUE); $fields['name'] = BaseFieldDefinition::create('string') - ->setLabel(t('Your name')) + ->setLabel(t('Name')) + ->setDescription(t("The comment author's name.")) ->setTranslatable(TRUE) ->setSetting('max_length', 60) ->setDefaultValue('') @@ -288,11 +301,15 @@ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) { ->setTranslatable(TRUE) ->setSetting('max_length', 128); - // @todo Use timestamp widget after https://drupal.org/node/2226493 lands. $fields['created'] = BaseFieldDefinition::create('created') - ->setLabel(t('Created')) + ->setLabel(t('Authored on')) ->setDescription(t('The time that the comment was created.')) - ->setTranslatable(TRUE); + ->setTranslatable(TRUE) + ->setDisplayOptions('form', array( + 'type' => 'datetime_timestamp', + 'weight' => 10, + )) + ->setDisplayConfigurable('form', TRUE); $fields['changed'] = BaseFieldDefinition::create('changed') ->setLabel(t('Changed')) @@ -341,6 +358,18 @@ public static function bundleFieldDefinitions(EntityTypeInterface $entity_type, } /** + * Default value callback for 'uid' base field definition. + * + * @see ::baseFieldDefinitions() + * + * @return array + * An array of default values. + */ + public static function getCurrentUserId() { + return array(\Drupal::currentUser()->id()); + } + + /** * {@inheritdoc} */ public function hasParentComment() {