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 15bc2cc..9b3c8fb 100644 --- a/core/lib/Drupal/Core/Field/Plugin/Field/FieldWidget/EmailDefaultWidget.php +++ b/core/lib/Drupal/Core/Field/Plugin/Field/FieldWidget/EmailDefaultWidget.php @@ -28,6 +28,7 @@ class EmailDefaultWidget extends WidgetBase { */ public static function defaultSettings() { return array( + 'size' => 60, 'placeholder' => '', ) + parent::defaultSettings(); } @@ -70,6 +71,8 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen '#type' => 'email', '#default_value' => isset($items[$delta]->value) ? $items[$delta]->value : NULL, '#placeholder' => $this->getSetting('placeholder'), + '#size' => $this->getSetting('size'), + '#maxlength' => EMAIL_MAX_LENGTH, ); return $element; } diff --git a/core/modules/comment/src/CommentForm.php b/core/modules/comment/src/CommentForm.php index db4178d..3b800b3 100644 --- a/core/modules/comment/src/CommentForm.php +++ b/core/modules/comment/src/CommentForm.php @@ -75,6 +75,8 @@ protected function init(array &$form_state) { public function form(array $form, array &$form_state) { /** @var \Drupal\comment\CommentInterface $comment */ $comment = $this->entity; + $form = parent::form($form, $form_state, $comment); + $entity = $this->entityManager->getStorage($comment->getCommentedEntityTypeId())->load($comment->getCommentedEntityId()); $field_name = $comment->getFieldName(); $field_definition = $this->entityManager->getFieldDefinitions($entity->getEntityTypeId(), $entity->bundle())[$comment->getFieldName()]; @@ -112,7 +114,6 @@ public function form(array $form, array &$form_state) { // Prepare default values for form elements. if ($is_admin) { - $author = $comment->getAuthorName(); $status = $comment->isPublished(); if (empty($form_state['comment_preview'])) { $form['#title'] = $this->t('Edit comment %title', array( @@ -121,12 +122,6 @@ public function form(array $form, array &$form_state) { } } else { - if ($this->currentUser->isAuthenticated()) { - $author = $this->currentUser->getUsername(); - } - else { - $author = ($comment->getAuthorName() ? $comment->getAuthorName() : ''); - } $status = ($this->currentUser->hasPermission('skip comment approval') ? CommentInterface::PUBLISHED : CommentInterface::NOT_PUBLISHED); } @@ -136,24 +131,18 @@ public function form(array $form, array &$form_state) { } // Add the author name field depending on the current user. - $form['author']['name'] = array( - '#type' => 'textfield', - '#title' => $this->t('Your name'), - '#default_value' => $author, - '#required' => ($this->currentUser->isAnonymous() && $anonymous_contact == COMMENT_ANONYMOUS_MUST_CONTACT), - '#maxlength' => 60, - '#size' => 30, - ); + $form['name']['widget'][0]['value']['#required'] = ($this->currentUser->isAnonymous() && $anonymous_contact == COMMENT_ANONYMOUS_MUST_CONTACT); if ($is_admin) { - $form['author']['name']['#title'] = $this->t('Authored by'); - $form['author']['name']['#description'] = $this->t('Leave blank for %anonymous.', array('%anonymous' => $this->config('user.settings')->get('anonymous'))); - $form['author']['name']['#autocomplete_route_name'] = 'user.autocomplete'; + $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['author']['name']['#type'] = 'item'; - $form['author']['name']['#value'] = $form['author']['name']['#default_value']; - $form['author']['name']['#theme'] = 'username'; - $form['author']['name']['#account'] = $this->currentUser; + $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; } $language_configuration = \Drupal::moduleHandler()->invoke('language', 'get_default_configuration', array('comment', $comment->getTypeId())); @@ -166,25 +155,17 @@ public function form(array $form, array &$form_state) { ); // Add author email and homepage fields depending on the current user. - $form['author']['mail'] = array( - '#type' => 'email', - '#title' => $this->t('Email'), - '#default_value' => $comment->getAuthorEmail(), - '#required' => ($this->currentUser->isAnonymous() && $anonymous_contact == COMMENT_ANONYMOUS_MUST_CONTACT), - '#maxlength' => 64, - '#size' => 30, - '#description' => $this->t('The content of this field is kept private and will not be shown publicly.'), - '#access' => $is_admin || ($this->currentUser->isAnonymous() && $anonymous_contact != COMMENT_ANONYMOUS_MAYNOT_CONTACT), - ); + // @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']['#access'] = $is_admin || ($this->currentUser->isAnonymous() && $anonymous_contact != COMMENT_ANONYMOUS_MAYNOT_CONTACT); + if ($is_admin) { + $form['mail']['#group'] = 'author'; + } - $form['author']['homepage'] = array( - '#type' => 'url', - '#title' => $this->t('Homepage'), - '#default_value' => $comment->getHomepage(), - '#maxlength' => 255, - '#size' => 30, - '#access' => $is_admin || ($this->currentUser->isAnonymous() && $anonymous_contact != COMMENT_ANONYMOUS_MAYNOT_CONTACT), - ); + $form['homepage']['#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( @@ -212,7 +193,7 @@ public function form(array $form, array &$form_state) { '#value' => ($comment->id() ? !$comment->getOwnerId() : $this->currentUser->isAnonymous()), ); - return parent::form($form, $form_state, $comment); + return $form; } /** diff --git a/core/modules/comment/src/Entity/Comment.php b/core/modules/comment/src/Entity/Comment.php index 8ca87f5..f6756b1 100644 --- a/core/modules/comment/src/Entity/Comment.php +++ b/core/modules/comment/src/Entity/Comment.php @@ -242,25 +242,43 @@ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) { ->setDefaultValue(0); $fields['name'] = FieldDefinition::create('string') - ->setLabel(t('Name')) - ->setDescription(t("The comment author's name.")) + ->setLabel(t('Your name')) ->setTranslatable(TRUE) ->setSetting('max_length', 60) ->setDefaultValue('') - ->addConstraint('CommentName', array()); + ->addConstraint('CommentName', array()) + ->setDisplayOptions('form', array( + 'type' => 'string', + 'weight' => -15, + 'settings' => array( + 'size' => 30, + ), + )) + ->setDisplayConfigurable('form', TRUE); $fields['mail'] = FieldDefinition::create('email') ->setLabel(t('Email')) - ->setDescription(t("The comment author's email address.")) - ->setTranslatable(TRUE); + ->setDescription(t('The content of this field is kept private and will not be shown publicly.')) + ->setTranslatable(TRUE) + ->setDisplayOptions('form', array( + 'type' => 'email_default', + 'weight' => -10, + 'settings' => array( + 'size' => 30, + ), + )); $fields['homepage'] = FieldDefinition::create('uri') ->setLabel(t('Homepage')) - ->setDescription(t("The comment author's home page address.")) ->setTranslatable(TRUE) // URIs are not length limited by RFC 2616, but we can only store 255 // characters in our comment DB schema. - ->setSetting('max_length', 255); + ->setSetting('max_length', 255) + ->setDisplayOptions('form', array( + 'type' => 'uri', + 'size' => 30, + 'weight' => -5, + )); $fields['hostname'] = FieldDefinition::create('string') ->setLabel(t('Hostname')) @@ -268,6 +286,7 @@ 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'] = FieldDefinition::create('created') ->setLabel(t('Created')) ->setDescription(t('The time that the comment was created.'))