Commit 67ffbd35 authored by alexpott's avatar alexpott
Browse files

Issue #1987860 by tim.plunkett: Convert taxonomy_term_add() to a new style controller.

parent be502b46
<?php
/**
* @file
* Contains \Drupal\taxonomy\Access\TaxonomyTermCreateAccess.
*/
namespace Drupal\taxonomy\Access;
use Drupal\Core\Entity\EntityCreateAccessCheck;
use Symfony\Component\HttpFoundation\Request;
/**
* Provides an access check for taxonomy term creation.
*/
class TaxonomyTermCreateAccess extends EntityCreateAccessCheck {
/**
* {@inheritdoc}
*/
protected $requirementsKey = '_access_taxonomy_term_create';
/**
* {@inheritdoc}
*/
protected function prepareEntityValues(array $definition, Request $request, $bundle = NULL) {
$values = array();
if ($vocabulary = $request->attributes->get('taxonomy_vocabulary')) {
$values = parent::prepareEntityValues($definition, $request, $vocabulary->id());
}
return $values;
}
}
<?php
/**
* @file
* Contains \Drupal\taxonomy\Controller\TaxonomyController.
*/
namespace Drupal\taxonomy\Controller;
use Drupal\Core\Controller\ControllerInterface;
use Drupal\Core\Entity\EntityManager;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\taxonomy\TermStorageControllerInterface;
use Drupal\taxonomy\VocabularyInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Provides route responses for taxonomy.module.
*/
class TaxonomyController implements ControllerInterface {
/**
* The module handler.
*
* @var \Drupal\Core\Extension\ModuleHandlerInterface
*/
protected $moduleHandler;
/**
* The entity manager.
*
* @var \Drupal\Core\Entity\EntityManager
*/
protected $entityManager;
/**
* The taxonomy term storage.
*
* @var \Drupal\taxonomy\TermStorageControllerInterface
*/
protected $termStorage;
/**
* Constructs a new TaxonomyController.
*
* @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
* The module handler.
* @param \Drupal\Core\Entity\EntityManager $entity_manager
* The entity manager.
* @param \Drupal\taxonomy\TermStorageControllerInterface $term_storage
* The taxonomy term storage.
*/
public function __construct(ModuleHandlerInterface $module_handler, EntityManager $entity_manager, TermStorageControllerInterface $term_storage) {
$this->moduleHandler = $module_handler;
$this->entityManager = $entity_manager;
$this->termStorage = $term_storage;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
$entity_manager = $container->get('plugin.manager.entity');
return new static(
$container->get('module_handler'),
$entity_manager,
$entity_manager->getStorageController('taxonomy_term')
);
}
/**
* Returns a rendered edit form to create a new term associated to the given vocabulary.
*
* @param \Drupal\taxonomy\VocabularyInterface $taxonomy_vocabulary
* The vocabulary this term will be added to.
*
* @return array
* The taxonomy term add form.
*/
public function addForm(VocabularyInterface $taxonomy_vocabulary) {
$term = $this->termStorage->create(array('vid' => $taxonomy_vocabulary->id()));
if ($this->moduleHandler->moduleExists('language')) {
$term->langcode = language_get_default_langcode('taxonomy_term', $taxonomy_vocabulary->id());
}
return $this->entityManager->getForm($term);
}
}
......@@ -390,17 +390,6 @@ function taxonomy_overview_terms_submit($form, &$form_state) {
drupal_set_message(t('The configuration options have been saved.'));
}
/**
* Returns a rendered edit form to create a new term associated to the given vocabulary.
*/
function taxonomy_term_add($vocabulary) {
$term = entity_create('taxonomy_term', array('vid' => $vocabulary->id()));
if (module_exists('language')) {
$term->langcode = language_get_default_langcode('taxonomy_term', $vocabulary->id());
}
return Drupal::entityManager()->getForm($term);
}
/**
* Form builder for the term delete form.
*
......
......@@ -329,30 +329,13 @@ function taxonomy_menu() {
$items['admin/structure/taxonomy/manage/%taxonomy_vocabulary/add'] = array(
'title' => 'Add term',
'page callback' => 'taxonomy_term_add',
'page arguments' => array(4),
'access callback' => 'taxonomy_term_create_access',
'access arguments' => array(4),
'route_name' => 'taxonomy_term_add',
'type' => MENU_LOCAL_ACTION,
'file' => 'taxonomy.admin.inc',
);
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().
*/
......
......@@ -4,3 +4,10 @@ taxonomy_vocabulary_list:
_entity_list: 'taxonomy_vocabulary'
requirements:
_permission: 'administer taxonomy'
taxonomy_term_add:
pattern: '/admin/structure/taxonomy/manage/{taxonomy_vocabulary}/add'
defaults:
_content: '\Drupal\taxonomy\Controller\TaxonomyController::addForm'
requirements:
_access_taxonomy_term_create: 'taxonomy_term'
services:
access_check.taxonomy_term.create:
class: Drupal\taxonomy\Access\TaxonomyTermCreateAccess
arguments: ['@plugin.manager.entity']
tags:
- { name: access_check }
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