Skip to content
Snippets Groups Projects
Commit f33b5f0f authored by Viktor Holovachek's avatar Viktor Holovachek
Browse files

Resolve #3412501 "Access"

parent f4bfc878
No related branches found
No related tags found
1 merge request!6Resolve #3412501 "Access"
......@@ -7,6 +7,7 @@
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Routing\RouteMatchInterface;
/**
......@@ -53,6 +54,21 @@ function node_display_title_entity_base_field_info(EntityTypeInterface $entity_t
return $fields;
}
/**
* Implements hook_form_BASE_FORM_ID_alter().
*/
function node_display_title_form_node_form_alter(&$form, FormStateInterface $form_state, $form_id) {
if (array_key_exists('display_title', $form)) {
/** @var \Drupal\node\NodeInterface $node */
$node = $form_state->getFormObject()->getEntity();
$current_user = \Drupal::currentUser();
$access = $current_user->hasPermission('access display title field') || $current_user->hasPermission("access {$node->bundle()} display title field");
$form['display_title']['#access'] = $access;
}
}
/**
* Implements hook_ENTITY_TYPE_load().
*/
......
access display title field:
title: 'Access any display title field'
description: 'Allows a user to access display title field on the form.'
permission_callbacks:
- Drupal\node_display_title\NodeDisplayTitlePermissions::permissions
<?php
namespace Drupal\node_display_title;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\node\Entity\NodeType;
/**
* Provides dynamic permissions for node display title module.
*/
class NodeDisplayTitlePermissions {
use StringTranslationTrait;
/**
* Returns an array of node display title permissions.
*
* @return array
* An array of permissions for node types.
*/
public function permissions() {
$permissions = [];
foreach (NodeType::loadMultiple() as $node_type) {
$permissions["access {$node_type->id()} display title field"] = [
'title' => $this->t('Access @node_type display title field', ['@node_type' => $node_type->label()]),
];
}
return $permissions;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment