I'm a beginner with D8 but wrote own modules in D7. It is therefore possible that I miss some understanding to get easily what I need. All hints to help me step ahead are appreciated.
When I add a comment field to a node entity I have the possibility to configure for anonymous users between the following options:
See CommentItem.php
function fieldSettingsForm(array $form, FormStateInterface $form_state) ( ...
$element['anonymous'] = array(
'#type' => 'select',
'#title' => t('Anonymous commenting'),
'#default_value' => $settings['anonymous'],
'#options' => array(
COMMENT_ANONYMOUS_MAYNOT_CONTACT => t('Anonymous posters may not enter their contact information'),
COMMENT_ANONYMOUS_MAY_CONTACT => t('Anonymous posters may leave their contact information'),
COMMENT_ANONYMOUS_MUST_CONTACT => t('Anonymous posters must leave their contact information'),
),
'#access' => $anonymous_user->hasPermission('post comments'),
);
In CommentForm.php
function form()...
// 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' => ($comment->getOwner()->isAnonymous() && $is_admin) || ($this->currentUser->isAnonymous() && $anonymous_contact != COMMENT_ANONYMOUS_MAYNOT_CONTACT),
);
$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),
);
I see the COMMENT_ANNONYMOUS_MAY_CONTACT or COMMENT_ANONYMOUS_MUST_CONTACT is used to set the field access option and the COMMENT_ANONYMOUS_MUST_CONTACT is used to set the field required option. Therefore it is not possible to set the configuration in such I way to get rid of the homepage field, since this is accessible and not required in both cases.
In my case I want to have the contact information where the name and e-mail are required without the homepage field. Unfortunately there is no possibility to change this within the manage field tab.
I thought the best solution would be to have the possibility to make show up the fields name, e-mail and homepage in the manage form display tab as soon as the value in the comment settings form is set to either COMMENT_ANONYMOUS_MUST_CONTACT or COMMENT_ANONYMOUS_MAY_CONTACT in order to set them as deactivated if not needed.
The idea was to use hook_entity_base_field_info_alter()
to set the homepage field with ..->setDisplayConfigurable('form', TRUE)
to become configurable. But this does not seem to work. In addition I see in entity.api.php several warings about upcoming needed changes.
Current solution is to use
function hook_form_comment_comment_form_alter(&$form, Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
$form['author']['homepage']['#access'] = FALSE;
}
I think that others would appreciate to have this ability too and maybe there should be added a change request into core. Looking at this I don't really know where this change request should be added (comment module? field entity? content entity? other?). Any suggestions are appreciated