Commit 17f4d142 authored by alexpott's avatar alexpott

Issue #1862758 by Berdir, dawehner: [Followup] Implement entity access API for...

Issue #1862758 by Berdir, dawehner: [Followup] Implement entity access API for terms and vocabularies.
parent 82cf432a
......@@ -753,18 +753,25 @@ function entity_page_access(EntityInterface $entity, $operation = 'view') {
/**
* Generic access callback for create entity pages.
*
* Some entity types might have create access per bundle or something else.
* In that case you have to create a custom access callback.
*
* @param string $entity_type
* The entity type.
* @param string $bundle
* (optional) The bundle of the entity. Required if the entity supports
* bundles, defaults to the entity type otherwise.
*
* @return bool
* TRUE if the access is granted. FALSE if access is denied.
*/
function entity_page_create_access($entity_type) {
function entity_page_create_access($entity_type, $bundle = NULL) {
$definition = Drupal::service('plugin.manager.entity')->getDefinition($entity_type);
// Pass in the entity bundle if given and required.
$values = array();
if ($bundle && isset($definition['entity_keys']['bundle'])) {
$values[$definition['entity_keys']['bundle']] = $bundle;
}
$entity = drupal_container()->get('plugin.manager.entity')
->getStorageController($entity_type)
->create(array());
->create($values);
return $entity->access('create');
}
......@@ -359,8 +359,8 @@ function taxonomy_menu() {
'title' => 'Add term',
'page callback' => 'taxonomy_term_add',
'page arguments' => array(3),
'access callback' => 'entity_page_create_access',
'access arguments' => array('taxonomy_term'),
'access callback' => 'taxonomy_term_create_access',
'access arguments' => array(3),
'type' => MENU_LOCAL_ACTION,
'file' => 'taxonomy.admin.inc',
);
......@@ -368,6 +368,19 @@ function taxonomy_menu() {
return $items;
}
/**
* Access callback for creating a new taxonomy term.
*
* @param \Drupal\taxonomy\Plugin\Core\Entity\Vocabulary $vocabulary
* The vocabulary entity that represents the term bundle.
*
* @return bool
* TRUE if access is allowed, FALSE if not.
*/
function taxonomy_term_create_access(Vocabulary $vocabulary) {
return entity_page_create_access('taxonomy_term', $vocabulary->id());
}
/**
* Implements hook_admin_paths().
*/
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment