Commit 98858def authored by Dries's avatar Dries

- Patch #1533022 by duellj, aspilicious: convert taxonomy.module entity classes to PSR-0.

parent 99243487
......@@ -208,8 +208,8 @@ public function load($ids = array(), $conditions = array()) {
* being loaded needs to be augmented with additional data from another
* table, such as loading node type into comments or vocabulary machine name
* into terms, however it can also support $conditions on different tables.
* See CommentController::buildQuery() or TaxonomyTermController::buildQuery()
* for examples.
* See Drupal\comment\CommentStorageController::buildQuery() or
* Drupal\taxonomy\TermStorageController::buildQuery() for examples.
*
* @param $ids
* An array of entity IDs, or FALSE to load all entities.
......
......@@ -6,6 +6,7 @@
*/
use Drupal\node\Node;
use Drupal\taxonomy\Term;
/**
* Implements hook_help().
......@@ -470,7 +471,7 @@ function forum_permission() {
/**
* Implements hook_taxonomy_term_delete().
*/
function forum_taxonomy_term_delete(TaxonomyTerm $term) {
function forum_taxonomy_term_delete(Term $term) {
// For containers, remove the tid from the forum_containers variable.
$containers = variable_get('forum_containers', array());
$key = array_search($term->tid, $containers);
......
......@@ -6,6 +6,7 @@
*/
use Drupal\node\Node;
use Drupal\taxonomy\Term;
/**
* Implements hook_help().
......@@ -268,7 +269,7 @@ function path_form_taxonomy_form_term_alter(&$form, $form_state) {
/**
* Implements hook_taxonomy_term_insert().
*/
function path_taxonomy_term_insert(TaxonomyTerm $term) {
function path_taxonomy_term_insert(Term $term) {
if (isset($term->path)) {
$path = $term->path;
$path['alias'] = trim($path['alias']);
......@@ -285,7 +286,7 @@ function path_taxonomy_term_insert(TaxonomyTerm $term) {
/**
* Implements hook_taxonomy_term_update().
*/
function path_taxonomy_term_update(TaxonomyTerm $term) {
function path_taxonomy_term_update(Term $term) {
if (isset($term->path)) {
$path = $term->path;
$path['alias'] = trim($path['alias']);
......@@ -306,7 +307,7 @@ function path_taxonomy_term_update(TaxonomyTerm $term) {
/**
* Implements hook_taxonomy_term_delete().
*/
function path_taxonomy_term_delete(TaxonomyTerm $term) {
function path_taxonomy_term_delete(Term $term) {
// Delete all aliases associated with this term.
path_delete(array('source' => 'taxonomy/term/' . $term->tid));
}
......@@ -5,6 +5,8 @@
* Test module for Taxonomy hooks and functions not used in core.
*/
use Drupal\taxonomy\Term;
/**
* Implements hook_taxonomy_term_load().
*/
......@@ -20,7 +22,7 @@ function taxonomy_test_taxonomy_term_load(array $terms) {
/**
* Implements hook_taxonomy_term_insert().
*/
function taxonomy_test_taxonomy_term_insert(TaxonomyTerm $term) {
function taxonomy_test_taxonomy_term_insert(Term $term) {
if (!empty($term->antonym)) {
db_insert('taxonomy_term_antonym')
->fields(array(
......@@ -34,7 +36,7 @@ function taxonomy_test_taxonomy_term_insert(TaxonomyTerm $term) {
/**
* Implements hook_taxonomy_term_update().
*/
function taxonomy_test_taxonomy_term_update(TaxonomyTerm $term) {
function taxonomy_test_taxonomy_term_update(Term $term) {
if (!empty($term->antonym)) {
db_merge('taxonomy_term_antonym')
->key(array('tid' => $term->tid))
......@@ -48,7 +50,7 @@ function taxonomy_test_taxonomy_term_update(TaxonomyTerm $term) {
/**
* Implements hook_taxonomy_term_delete().
*/
function taxonomy_test_taxonomy_term_delete(TaxonomyTerm $term) {
function taxonomy_test_taxonomy_term_delete(Term $term) {
db_delete('taxonomy_term_antonym')
->condition('tid', $term->tid)
->execute();
......
<?php
/**
* @file
* Definition of Term.
*/
namespace Drupal\taxonomy;
use Drupal\entity\Entity;
/**
* Defines the taxonomy term entity.
*/
class Term extends Entity {
/**
* The taxonomy term ID.
*
* @var integer
*/
public $tid;
/**
* The taxonomy vocabulary ID this term belongs to.
*
* @var integer
*/
public $vid;
/**
* Name of the term.
*
* @var string
*/
public $name;
/**
* Description of the term.
*
* @var string
*/
public $description;
/**
* The text format name for the term's description.
*
* @var string
*/
public $format;
/**
* The weight of this term.
*
* This property stores the weight of this term in relation to other terms of
* the same vocabulary.
*
* @var integer
*/
public $weight = 0;
/**
* The parent term(s) for this term.
*
* This property is not loaded, but may be used to modify the term parents via
* Term::save().
*
* The property can be set to an array of term IDs. An entry of 0 means this
* term does not have any parents. When omitting this variable during an
* update, the existing hierarchy for the term remains unchanged.
*
* @var array
*/
public $parent;
/**
* The machine name of the vocabulary the term is assigned to.
*
* If not given, this value will be set automatically by loading the
* vocabulary based on the $entity->vid property.
*
* @var string
*/
public $vocabulary_machine_name;
/**
* Implements Drupal\entity\EntityInterface::id().
*/
public function id() {
return $this->tid;
}
/**
* Implements Drupal\entity\EntityInterface::bundle().
*/
public function bundle() {
return $this->vocabulary_machine_name;
}
}
......@@ -2,106 +2,21 @@
/**
* @file
* Entity classes and controllers for Taxonomy module.
* Definition of TermStorageController.
*/
use Drupal\entity\Entity;
namespace Drupal\taxonomy;
use Drupal\entity\EntityInterface;
use Drupal\entity\EntityDatabaseStorageController;
/**
* Defines the taxonomy term entity.
* Defines a Controller class for taxonomy terms.
*/
class TaxonomyTerm extends Entity {
/**
* The taxonomy term ID.
*
* @var integer
*/
public $tid;
/**
* The taxonomy vocabulary ID this term belongs to.
*
* @var integer
*/
public $vid;
/**
* Name of the term.
*
* @var string
*/
public $name;
/**
* (optional) Description of the term.
*
* @var string
*/
public $description;
/**
* (optional) The text format name for the term's description.
*
* @var string
*/
public $format;
/**
* (optional) The weight of this term in relation to other terms of the same
* vocabulary.
*
* @var integer
*/
public $weight = 0;
/**
* (optional) The parent term(s) for this term.
*
* This property is not loaded, but may be used to modify the term parents via
* TaxonomyTerm::save().
*
* The property can be set to an array of term IDs. An entry of 0 means this
* term does not have any parents. When omitting this variable during an
* update, the existing hierarchy for the term remains unchanged.
*
* @var array
*/
public $parent;
class TermStorageController extends EntityDatabaseStorageController {
/**
* (optional) The machine name of the vocabulary the term is assigned to. If
* not given, this value will be set automatically by loading the vocabulary
* based on the $entity->vid property.
*
* @var string
*/
public $vocabulary_machine_name;
/**
* Implements EntityInterface::id().
*/
public function id() {
return $this->tid;
}
/**
* Implements EntityInterface::bundle().
*/
public function bundle() {
return $this->vocabulary_machine_name;
}
}
/**
* Controller class for taxonomy terms.
*/
class TaxonomyTermController extends EntityDatabaseStorageController {
/**
* Overrides EntityDatabaseStorageController::create().
* Overrides Drupal\entity\EntityDatabaseStorageController::create().
*
* @param array $values
* An array of values to set, keyed by property name. A value for the
......@@ -109,10 +24,10 @@ class TaxonomyTermController extends EntityDatabaseStorageController {
*/
public function create(array $values) {
$entity = parent::create($values);
// Ensure the vocabulary machine name is initialized as it is used as bundle
// key.
// @todo Move to TaxonomyTerm::bundle() once field API has been converted
// to make use of it.
// Ensure the vocabulary machine name is initialized as it is used as the
// bundle key.
// @todo Move to Term::bundle() once field API has been converted
// to make use of it.
if (!isset($entity->vocabulary_machine_name)) {
$vocabulary = taxonomy_vocabulary_load($entity->vid);
$entity->vocabulary_machine_name = $vocabulary->machine_name;
......@@ -125,7 +40,7 @@ public function create(array $values) {
}
/**
* Overrides EntityDatabaseStorageController::buildQuery().
* Overrides Drupal\entity\EntityDatabaseStorageController::buildQuery().
*/
protected function buildQuery($ids, $conditions = array(), $revision_id = FALSE) {
$query = parent::buildQuery($ids, $conditions, $revision_id);
......@@ -148,7 +63,7 @@ protected function buildQuery($ids, $conditions = array(), $revision_id = FALSE)
}
/**
* Overrides EntityDatabaseStorageController::cacheGet().
* Overrides Drupal\entity\EntityDatabaseStorageController::cacheGet().
*/
protected function cacheGet($ids, $conditions = array()) {
$terms = parent::cacheGet($ids, $conditions);
......@@ -163,7 +78,7 @@ protected function cacheGet($ids, $conditions = array()) {
}
/**
* Overrides EntityDatabaseStorageController::postDelete().
* Overrides Drupal\entity\EntityDatabaseStorageController::postDelete().
*/
protected function postDelete($entities) {
// See if any of the term's children are about to be become orphans.
......@@ -194,7 +109,7 @@ protected function postDelete($entities) {
}
/**
* Overrides EntityDatabaseStorageController::postSave().
* Overrides Drupal\entity\EntityDatabaseStorageController::postSave().
*/
protected function postSave(EntityInterface $entity, $update) {
if (isset($entity->parent)) {
......@@ -229,141 +144,3 @@ public function resetCache(array $ids = NULL) {
parent::resetCache($ids);
}
}
/**
* Defines the taxonomy vocabulary entity.
*/
class TaxonomyVocabulary extends Entity {
/**
* The taxonomy vocabulary ID.
*
* @var integer
*/
public $vid;
/**
* Name of the vocabulary.
*
* @var string
*/
public $name;
/**
* The vocabulary machine name.
*
* @var string
*/
public $machine_name;
/**
* (optional) Description of the vocabulary.
*
* @var string
*/
public $description;
/**
* The type of hierarchy allowed within the vocabulary.
*
* Possible values:
* - TAXONOMY_HIERARCHY_DISABLED: No parents.
* - TAXONOMY_HIERARCHY_SINGLE: Single parent.
* - TAXONOMY_HIERARCHY_MULTIPLE: Multiple parents.
*
* @var integer
*/
public $hierarchy = TAXONOMY_HIERARCHY_DISABLED;
/**
* (optional) The weight of this vocabulary in relation to other vocabularies.
*
* @var integer
*/
public $weight = 0;
/**
* Implements EntityInterface::id().
*/
public function id() {
return $this->vid;
}
}
/**
* Controller class for taxonomy vocabularies.
*/
class TaxonomyVocabularyController extends EntityDatabaseStorageController {
/**
* Overrides EntityDatabaseStorageController::buildQuery().
*/
protected function buildQuery($ids, $conditions = array(), $revision_id = FALSE) {
$query = parent::buildQuery($ids, $conditions, $revision_id);
$query->addTag('translatable');
$query->orderBy('base.weight');
$query->orderBy('base.name');
return $query;
}
/**
* Overrides EntityDatabaseStorageController::postSave().
*/
protected function postSave(EntityInterface $entity, $update) {
if (!$update) {
field_attach_create_bundle('taxonomy_term', $entity->machine_name);
}
elseif ($entity->original->machine_name != $entity->machine_name) {
field_attach_rename_bundle('taxonomy_term', $entity->original->machine_name, $entity->machine_name);
}
}
/**
* Overrides EntityDatabaseStorageController::preDelete().
*/
protected function preDelete($entities) {
// Only load terms without a parent, child terms will get deleted too.
$tids = db_query('SELECT t.tid FROM {taxonomy_term_data} t INNER JOIN {taxonomy_term_hierarchy} th ON th.tid = t.tid WHERE t.vid IN (:vids) AND th.parent = 0', array(':vids' => array_keys($entities)))->fetchCol();
taxonomy_term_delete_multiple($tids);
}
/**
* Overrides EntityDatabaseStorageController::postDelete().
*/
protected function postDelete($entities) {
// Load all Taxonomy module fields and delete those which use only this
// vocabulary.
$taxonomy_fields = field_read_fields(array('module' => 'taxonomy'));
foreach ($taxonomy_fields as $field_name => $taxonomy_field) {
$modified_field = FALSE;
// Term reference fields may reference terms from more than one
// vocabulary.
foreach ($taxonomy_field['settings']['allowed_values'] as $key => $allowed_value) {
foreach ($entities as $vocabulary) {
if ($allowed_value['vocabulary'] == $vocabulary->machine_name) {
unset($taxonomy_field['settings']['allowed_values'][$key]);
$modified_field = TRUE;
}
}
}
if ($modified_field) {
if (empty($taxonomy_field['settings']['allowed_values'])) {
field_delete_field($field_name);
}
else {
// Update the field definition with the new allowed values.
field_update_field($taxonomy_field);
}
}
}
}
/**
* Implements Drupal\entity\EntityControllerInterface::resetCache().
*/
public function resetCache(array $ids = NULL) {
drupal_static_reset('taxonomy_vocabulary_get_names');
parent::resetCache($ids);
cache_clear_all();
}
}
<?php
/**
* @file
* Definition of Vocabulary.
*/
namespace Drupal\taxonomy;
use Drupal\entity\Entity;
/**
* Defines the taxonomy vocabulary entity.
*/
class Vocabulary extends Entity {
/**
* The taxonomy vocabulary ID.
*
* @var integer
*/
public $vid;
/**
* Name of the vocabulary.
*
* @var string
*/
public $name;
/**
* The vocabulary machine name.
*
* @var string
*/
public $machine_name;
/**
* Description of the vocabulary.
*
* @var string
*/
public $description;
/**
* The type of hierarchy allowed within the vocabulary.
*
* Possible values:
* - TAXONOMY_HIERARCHY_DISABLED: No parents.
* - TAXONOMY_HIERARCHY_SINGLE: Single parent.
* - TAXONOMY_HIERARCHY_MULTIPLE: Multiple parents.
*
* @var integer
*/
public $hierarchy = TAXONOMY_HIERARCHY_DISABLED;
/**
* The weight of this vocabulary in relation to other vocabularies.
*
* @var integer
*/
public $weight = 0;
/**
* Implements Drupal\entity\EntityInterface::id().
*/
public function id() {
return $this->vid;
}
}
<?php
/**
* @file
* Definition of VocabularyStorageController.
*/
namespace Drupal\taxonomy;
use Drupal\entity\EntityInterface;
use Drupal\entity\EntityDatabaseStorageController;
/**
* Defines a controller class for taxonomy vocabularies.
*/
class VocabularyStorageController extends EntityDatabaseStorageController {
/**
* Overrides Drupal\entity\EntityDatabaseStorageController::buildQuery().
*/
protected function buildQuery($ids, $conditions = array(), $revision_id = FALSE) {
$query = parent::buildQuery($ids, $conditions, $revision_id);
$query->addTag('translatable');
$query->orderBy('base.weight');
$query->orderBy('base.name');
return $query;
}
/**
* Overrides Drupal\entity\EntityDatabaseStorageController::postSave().
*/
protected function postSave(EntityInterface $entity, $update) {
if (!$update) {
field_attach_create_bundle('taxonomy_term', $entity->machine_name);
}
elseif ($entity->original->machine_name != $entity->machine_name) {
field_attach_rename_bundle('taxonomy_term', $entity->original->machine_name, $entity->machine_name);
}
}
/**
* Overrides Drupal\entity\EntityDatabaseStorageController::preDelete().
*/
protected function preDelete($entities) {
// Only load terms without a parent, child terms will get deleted too.
$tids = db_query('SELECT t.tid FROM {taxonomy_term_data} t INNER JOIN {taxonomy_term_hierarchy} th ON th.tid = t.tid WHERE t.vid IN (:vids) AND th.parent = 0', array(':vids' => array_keys($entities)))->fetchCol();
taxonomy_term_delete_multiple($tids);
}
/**
* Overrides Drupal\entity\EntityDatabaseStorageController::postDelete().
*/
protected function postDelete($entities) {
// Load all Taxonomy module fields and delete those which use only this
// vocabulary.
$taxonomy_fields = field_read_fields(array('module' => 'taxonomy'));
foreach ($taxonomy_fields as $field_name => $taxonomy_field) {
$modified_field = FALSE;
// Term reference fields may reference terms from more than one
// vocabulary.
foreach ($taxonomy_field['settings']['allowed_values'] as $key => $allowed_value) {
foreach ($entities as $vocabulary) {
if ($allowed_value['vocabulary'] == $vocabulary->machine_name) {
unset($taxonomy_field['settings']['allowed_values'][$key]);
$modified_field = TRUE;
}
}
}
if ($modified_field) {
if (empty($taxonomy_field['settings']['allowed_values'])) {
field_delete_field($field_name);
}
else {
// Update the field definition with the new allowed values.
field_update_field($taxonomy_field);
}
}
}
}
/**
* Implements Drupal\entity\DrupalEntityControllerInterface::resetCache().
*/
public function resetCache(array $ids = NULL) {
drupal_static_reset('taxonomy_vocabulary_get_names');
parent::resetCache($ids);
cache_clear_all();
}
}
......@@ -5,6 +5,9 @@
* Administrative page callbacks for the taxonomy module.
*/
use Drupal\taxonomy\Term;
use Drupal\taxonomy\Vocabulary;
/**
* Form builder to list and manage vocabularies.
*
......@@ -101,7 +104,7 @@ function theme_taxonomy_overview_vocabularies($variables) {
/**
* Form builder for the vocabulary editing form.
*
* @param TaxonomyVocabulary|null $vocabulary
* @param Drupal\taxonomy\Vocabulary|null $vocabulary
* (optional) The taxonomy vocabulary entity to edit. If NULL or omitted, the
* form creates a new vocabulary.
*
......@@ -109,12 +112,12 @@ function theme_taxonomy_overview_vocabularies($variables) {
* @see taxonomy_form_vocabulary_submit()
* @see taxonomy_form_vocabulary_validate()
*/
function taxonomy_form_vocabulary($form, &$form_state, TaxonomyVocabulary $vocabulary = NULL) {
function taxonomy_form_vocabulary($form, &$form_state, Vocabulary $vocabulary = NULL) {
// During initial form build, add the entity to the form state for use
// during form building and processing. During a rebuild, use what is in the
// form state.
if (!isset($form_state['vocabulary'])) {
// Create a new TaxonomyVocabulary entity for the add form.
// Create a new Vocabulary entity for the add form.
if (!isset($vocabulary)) {
$vocabulary = entity_create('taxonomy_vocabulary', array(
// Default the new vocabulary to the site's default language. This is
......@@ -241,14 +244,14 @@ function taxonomy_form_vocabulary_submit($form, &$form_state) {
* Display a tree of all the terms in a vocabulary, with options to edit
* each one. The form is made drag and drop by the theme function.
*
* @param TaxonomyVocabulary $vocabulary
* @param Drupal\taxonomy\Vocabulary $vocabulary
* The taxonomy vocabulary entity to list terms for.
*
* @ingroup forms
* @see taxonomy_overview_terms_submit()
* @see theme_taxonomy_overview_terms()
*/
function taxonomy_overview_terms($form, &$form_state, TaxonomyVocabulary $vocabulary) {
function taxonomy_overview_terms($form, &$form_state, Vocabulary $vocabulary) {
global $pager_page_array, $pager_total, $pager_total_items;
// Check for confirmation forms.
......@@ -642,10 +645,10 @@ function theme_taxonomy_overview_terms($variables) {
/**
* Form function for the term edit form.
*
* @param TaxonomyTerm|null $term
* @param Drupal\taxonomy\Term|null $term
* (optional) The taxonomy term entity to edit. If NULL or omitted, the form
* creates a new term.
* @param TaxonomyVocabulary|null $vocabulary
* @param Drupal\taxonomy\Vocabulary|null $vocabulary
* (optional) A taxonomy vocabulary entity to create the term in. Required if
* the term is omitted.
*
......@@ -653,12 +656,12 @@ function theme_taxonomy_overview_terms($variables) {
* @see taxonomy_form_term_validate()
* @see taxonomy_form_term_submit()
*/
function taxonomy_form_term($form, &$form_state, TaxonomyTerm $term = NULL, TaxonomyVocabulary $vocabulary = NULL) {
function taxonomy_form_term($form, &$form_state, Term $term = NULL, Vocabulary $vocabulary = NULL) {
// During initial form build, add the term entity to the form state for use