function hide_revision_field_form_user_form_alter in Hide Revision Field 8
Same name and namespace in other branches
- 8.2 hide_revision_field.module \hide_revision_field_form_user_form_alter()
Implements hook_form_FORM_ID_alter().
Add the personalizable settings to an individual user's account page.
See also
\Drupal\user\ProfileForm::form()
File
- ./
hide_revision_field.module, line 87 - hide_revision_field.module Manages hiding revision information fields on revisionable entities (ie Node or Media) edit/create forms.
Code
function hide_revision_field_form_user_form_alter(&$form, FormStateInterface $form_state) {
// Ensure user has necessary permission.
if (!\Drupal::currentUser()
->hasPermission('administer personalized revision visibilty')) {
return;
}
$user_id = $form_state
->getFormObject()
->getEntity()
->id();
if (!isset($form['entity_options'])) {
$form['entity_options'] = [
'#type' => 'details',
'#title' => t('Entity Personalization'),
'#tree' => TRUE,
'#open' => FALSE,
'#weight' => 10,
];
}
$types = [
'node_type' => [
'title' => t('Content'),
'access' => 'node',
],
'media_bundle' => [
'title' => t('Media'),
'access' => 'media',
],
];
foreach ($types as $type => $type_data) {
// Ensure that the entity type exists for this install.
if (!\Drupal::entityTypeManager()
->hasDefinition($type)) {
continue;
}
if (!isset($form['entity_options'][$type])) {
$form['entity_options'][$type] = [
'#title' => $type_data['title'],
'#type' => 'details',
'#open' => TRUE,
];
}
foreach (\Drupal::entityTypeManager()
->getStorage($type)
->loadMultiple() as $bundle) {
// Filter by entity access permission.
if (!\Drupal::entityTypeManager()
->getAccessControlHandler($type_data['access'])
->createAccess($bundle
->id())) {
continue;
}
// Filter by entity settings.
if (!$bundle
->getThirdPartySetting('hide_revision_field', 'personalizable', TRUE)) {
continue;
}
$label = $bundle
->label();
$id = $bundle
->id();
$defaults = $bundle
->getThirdPartySetting('hide_revision_field', "personalization");
if (!isset($defaults[$user_id])) {
$defaults[$user_id] = [
'hide' => $bundle
->getThirdPartySetting('hide_revision_field', 'hide', FALSE),
];
}
if (!isset($form['entity_options'][$type][$id])) {
$form['entity_options'][$type][$id] = [
'#title' => $label,
'#type' => 'details',
'#tree' => TRUE,
'#open' => TRUE,
];
}
$form['entity_options'][$type][$id]['hide'] = [
'#type' => 'checkbox',
'#title' => t('Hide Revision Field'),
'#default_value' => $defaults[$user_id]['hide'],
];
}
}
$form['actions']['submit']['#submit'][] = 'hide_revision_field_user_profile_form_submit';
}