class TaxonomyTermController extends EntityDatabaseStorageController {
public function create(array $values) {
$entity = parent::create($values);
if (!isset($entity->parent)) {
$entity->parent = array(0);
}
return $entity;
}
protected function buildQuery($ids, $conditions = array(), $revision_id = FALSE) {
$query = parent::buildQuery($ids, $conditions, $revision_id);
$query->addTag('translatable');
$query->addTag('taxonomy_term_access');
if (isset($conditions['name'])) {
$query_conditions = &$query->conditions();
foreach ($query_conditions as $key => $condition) {
if (is_array($condition) && $condition['field'] == 'base.name') {
$query_conditions[$key]['operator'] = 'LIKE';
$query_conditions[$key]['value'] = db_like($query_conditions[$key]['value']);
}
}
}
return $query;
}
protected function cacheGet($ids, $conditions = array()) {
$terms = parent::cacheGet($ids, $conditions);
foreach ($terms as $term) {
if (isset($conditions['name']) && backdrop_strtolower($conditions['name'] != backdrop_strtolower($term->name))) {
unset($terms[$term->tid]);
}
}
return $terms;
}
protected function postDelete($entities) {
$orphans = array();
foreach (array_keys($entities) as $tid) {
if ($children = taxonomy_term_load_children($tid)) {
foreach ($children as $child) {
$parents = taxonomy_term_load_parents($child->tid);
if (count($parents) <= 1) {
$orphans[] = $child->tid;
}
}
}
}
db_delete('taxonomy_term_hierarchy')
->condition('tid', array_keys($entities))
->execute();
if (!empty($orphans)) {
taxonomy_term_delete_multiple($orphans);
}
}
protected function postSave(EntityInterface $entity, $update) {
if (isset($entity->parent)) {
db_delete('taxonomy_term_hierarchy')
->condition('tid', $entity->tid)
->execute();
$query = db_insert('taxonomy_term_hierarchy')
->fields(array('tid', 'parent'));
foreach ($entity->parent as $parent) {
$query->values(array(
'tid' => $entity->tid,
'parent' => $parent
));
}
$query->execute();
}
}
public function resetCache(array $ids = NULL) {
backdrop_static_reset('taxonomy_term_count_nodes');
backdrop_static_reset('taxonomy_get_tree');
backdrop_static_reset('taxonomy_get_tree:parents');
backdrop_static_reset('taxonomy_get_tree:terms');
backdrop_static_reset('taxonomy_term_load_parents');
backdrop_static_reset('taxonomy_term_load_parents_all');
backdrop_static_reset('taxonomy_term_load_children');
parent::resetCache($ids);
}
public function buildContent(EntityInterface $term, $view_mode = 'full', $langcode = NULL) {
global $language_content;
$langcode = $langcode ? $langcode : $language_content->langcode;
$term->content = array();
$view_mode = key(entity_view_mode_prepare('taxonomy_term', array($term->tid => $term), $view_mode, $langcode));
$type = 'taxonomy_term';
$entity_ids = entity_extract_ids($type, $term);
$settings = field_view_mode_settings($type, $entity_ids[2]);
$fields = field_extra_fields_get_display($type, $entity_ids[2], $view_mode);
if (!empty($term->description) && isset($fields['description']) && $fields['description']['visible']) {
$term->content['description'] = array(
'#markup' => check_markup($term->description, $term->format, '', TRUE),
'#weight' => $fields['description']['weight'],
'#prefix' => '<div class="taxonomy-term-description">',
'#suffix' => '</div>',
);
}
field_attach_prepare_view('taxonomy_term', array($term->tid => $term), $view_mode, $langcode);
entity_prepare_view('taxonomy_term', array($term->tid => $term), $langcode);
$term->content += field_attach_view('taxonomy_term', $term, $view_mode, $langcode);
module_invoke_all('taxonomy_term_view', $term, $view_mode, $langcode);
module_invoke_all('entity_view', $term, 'taxonomy_term', $view_mode, $langcode);
$term->content += array('#view_mode' => $view_mode);
}
public function view($terms, $view_mode = 'full', $langcode = NULL, $page = NULL) {
global $language_content;
$langcode = $langcode ? $langcode : $language_content->langcode;
$view = array();
foreach ($terms as $term) {
$this->buildContent($term, $view_mode, $langcode);
$build = $term->content;
unset($term->content);
$build += array(
'#theme' => 'taxonomy_term',
'#term' => $term,
'#view_mode' => $view_mode,
'#language' => $langcode,
'#page' => $page,
);
$build['#attached']['css'][] = backdrop_get_path('module', 'taxonomy') . '/css/taxonomy.css';
$type = 'taxonomy_term';
backdrop_alter(array('taxonomy_term_view', 'entity_view'), $build, $type);
$view[$type][$term->id()] = $build;
}
return $view;
}
}