Issue #1891690 by andypost: Use EntityListController for vocabularies.

8.0.x
Alex Pott 2013-06-09 15:29:54 +01:00
parent 4d2a10e036
commit 4370959d5e
5 changed files with 184 additions and 87 deletions

View File

@ -22,6 +22,7 @@ use Drupal\taxonomy\VocabularyInterface;
* controllers = {
* "storage" = "Drupal\taxonomy\VocabularyStorageController",
* "access" = "Drupal\taxonomy\VocabularyAccessController",
* "list" = "Drupal\taxonomy\VocabularyListController",
* "form" = {
* "default" = "Drupal\taxonomy\VocabularyFormController"
* }
@ -29,7 +30,8 @@ use Drupal\taxonomy\VocabularyInterface;
* config_prefix = "taxonomy.vocabulary",
* entity_keys = {
* "id" = "vid",
* "label" = "name"
* "label" = "name",
* "uuid" = "uuid"
* }
* )
*/
@ -42,6 +44,13 @@ class Vocabulary extends ConfigEntityBase implements VocabularyInterface {
*/
public $vid;
/**
* The vocabulary UUID.
*
* @var string
*/
public $uuid;
/**
* Name of the vocabulary.
*

View File

@ -0,0 +1,166 @@
<?php
/**
* @file
* Contains \Drupal\taxonomy\VocabularyListController.
*/
namespace Drupal\taxonomy;
use Drupal\Core\Config\Entity\ConfigEntityListController;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Form\FormInterface;
/**
* Provides a listing of vocabularies.
*/
class VocabularyListController extends ConfigEntityListController implements FormInterface {
/**
* {@inheritdoc}
*/
public function getFormID() {
return 'taxonomy_overview_vocabularies';
}
/**
* {@inheritdoc}
*/
public function getOperations(EntityInterface $entity) {
$operations = parent::getOperations($entity);
$uri = $entity->uri();
$operations['edit']['title'] = t('edit vocabulary');
$operations['list'] = array(
'title' => t('list terms'),
'href' => $uri['path'],
'options' => $uri['options'],
'weight' => 0,
);
$operations['add'] = array(
'title' => t('add terms'),
'href' => $uri['path'] . '/add',
'options' => $uri['options'],
'weight' => 30,
);
unset($operations['delete']);
return $operations;
}
/**
* {@inheritdoc}
*/
public function buildHeader() {
$row = parent::buildHeader();
$row['label'] = t('Vocabulary name');
unset($row['id']);
$row['weight'] = t('Weight');
return $row;
}
/**
* {@inheritdoc}
*/
public function buildRow(EntityInterface $entity) {
$row = parent::buildRow($entity);
// Override default values to markup elements.
$row['#attributes']['class'][] = 'draggable';
unset($row['id']);
$row['label'] = array(
'#markup' => check_plain($row['label']),
);
$row['#weight'] = $entity->get('weight');
// Add weight column.
$row['weight'] = array(
'#type' => 'weight',
'#title' => t('Weight for @title', array('@title' => $entity->label())),
'#title_display' => 'invisible',
'#default_value' => $entity->get('weight'),
'#attributes' => array('class' => array('weight')),
);
return $row;
}
/**
* {@inheritdoc}
*/
public function render() {
$entities = $this->load();
if (count($entities) > 1) {
// Creates a form for manipulating vocabulary weights if more then one
// vocabulary exists.
return drupal_get_form($this);
}
$build = array(
'#theme' => 'table',
'#header' => $this->buildHeader(),
'#rows' => array(),
'#empty' => t('No vocabularies available. <a href="@link">Add vocabulary</a>.', array('@link' => url('admin/structure/taxonomy/add'))),
);
unset($build['#header']['weight']);
foreach ($entities as $entity) {
$row = parent::buildRow($entity);
unset($row['id']);
$build['#rows'][$entity->id()] = $row;
}
return $build;
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, array &$form_state) {
$form['vocabularies'] = array(
'#type' => 'table',
'#header' => $this->buildHeader(),
'#tabledrag' => array(
array('order', 'sibling', 'weight'),
),
'#attributes' => array(
'id' => 'taxonomy',
),
);
foreach ($this->load() as $entity) {
$form['vocabularies'][$entity->id()] = $this->buildRow($entity);
}
$form['actions']['#type'] = 'actions';
$form['actions']['submit'] = array(
'#type' => 'submit',
'#value' => t('Save'),
'#button_type' => 'primary',
);
return $form;
}
/**
* {@inheritdoc}
*/
public function validateForm(array &$form, array &$form_state) {
// No validation.
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, array &$form_state) {
$vocabularies = $form_state['values']['vocabularies'];
$entities = entity_load_multiple($this->entityType, array_keys($vocabularies));
foreach ($vocabularies as $id => $value) {
if (isset($entities[$id]) && $value['weight'] != $entities[$id]->get('weight')) {
// Update changed weight.
$entities[$id]->set('weight', $value['weight']);
$entities[$id]->save();
}
}
drupal_set_message(t('The configuration options have been saved.'));
}
}

View File

@ -8,88 +8,6 @@
use Drupal\taxonomy\Plugin\Core\Entity\Term;
use Drupal\taxonomy\Plugin\Core\Entity\Vocabulary;
/**
* Form builder to list and manage vocabularies.
*
* @ingroup forms
* @see taxonomy_overview_vocabularies_submit()
* @see theme_taxonomy_overview_vocabularies()
*/
function taxonomy_overview_vocabularies($form) {
$vocabularies = taxonomy_vocabulary_load_multiple();
taxonomy_vocabulary_sort($vocabularies);
$form['vocabularies'] = array(
'#type' => 'table',
'#empty' => t('No vocabularies available. <a href="@link">Add vocabulary</a>.', array('@link' => url('admin/structure/taxonomy/add'))),
'#attributes' => array(
'id' => 'taxonomy',
),
);
foreach ($vocabularies as $vocabulary) {
$form['vocabularies'][$vocabulary->id()]['#vocabulary'] = $vocabulary;
$form['vocabularies'][$vocabulary->id()]['#attributes']['class'][] = 'draggable';
$form['vocabularies'][$vocabulary->id()]['name'] = array('#markup' => check_plain($vocabulary->name));
$form['vocabularies'][$vocabulary->id()]['weight'] = array(
'#type' => 'weight',
'#title' => t('Weight for @title', array('@title' => $vocabulary->name)),
'#title_display' => 'invisible',
'#delta' => 10,
'#default_value' => $vocabulary->weight,
'#attributes' => array(
'class' => array('vocabulary-weight'),
),
);
$links = array();
$links['edit'] = array(
'title' => t('edit vocabulary'),
'href' => "admin/structure/taxonomy/manage/{$vocabulary->id()}/edit",
);
$links['list'] = array(
'title' => t('list terms'),
'href' => "admin/structure/taxonomy/manage/{$vocabulary->id()}",
);
$links['add'] = array(
'title' => t('add terms'),
'href' => "admin/structure/taxonomy/manage/{$vocabulary->id()}/add",
);
$form['vocabularies'][$vocabulary->id()]['operations'] = array(
'#type' => 'operations',
'#links' => $links,
);
}
$form['vocabularies']['#header'] = array(t('Vocabulary name'));
// Only make this form include a submit button and weight if more than one
// vocabulary exists.
if (count($vocabularies) > 1) {
$form['vocabularies']['#tabledrag'][] = array('order', 'sibling', 'vocabulary-weight');
$form['vocabularies']['#header'][] = t('Weight');
$form['actions'] = array('#type' => 'actions');
$form['actions']['submit'] = array('#type' => 'submit', '#value' => t('Save'), '#button_type' => 'primary');
}
elseif (isset($vocabulary)) {
unset($form['vocabularies'][$vocabulary->id()]['weight']);
}
$form['vocabularies']['#header'][] = t('Operations');
return $form;
}
/**
* Submit handler for vocabularies overview. Updates changed vocabulary weights.
*
* @see taxonomy_overview_vocabularies()
*/
function taxonomy_overview_vocabularies_submit($form, &$form_state) {
foreach ($form_state['values']['vocabularies'] as $vid => $vocabulary) {
if (isset($form['vocabularies'][$vid]['#vocabulary']) && $form['vocabularies'][$vid]['#vocabulary']->weight != $form_state['values']['vocabularies'][$vid]['weight']) {
$form['vocabularies'][$vid]['#vocabulary']->weight = $form_state['values']['vocabularies'][$vid]['weight'];
$form['vocabularies'][$vid]['#vocabulary']->save();
}
}
drupal_set_message(t('The configuration options have been saved.'));
}
/**
* Page callback: provides the vocabulary creation form.
*/

View File

@ -232,10 +232,7 @@ function taxonomy_menu() {
$items['admin/structure/taxonomy'] = array(
'title' => 'Taxonomy',
'description' => 'Manage tagging, categorization, and classification of your content.',
'page callback' => 'drupal_get_form',
'page arguments' => array('taxonomy_overview_vocabularies'),
'access arguments' => array('administer taxonomy'),
'file' => 'taxonomy.admin.inc',
'route_name' => 'taxonomy_vocabulary_list',
);
$items['admin/structure/taxonomy/list'] = array(
'title' => 'List',

View File

@ -0,0 +1,7 @@
taxonomy_vocabulary_list:
pattern: '/admin/structure/taxonomy'
defaults:
_content: '\Drupal\Core\Entity\Controller\EntityListController::listing'
entity_type: 'taxonomy_vocabulary'
requirements:
_permission: 'administer taxonomy'