Узнать следующий/предыдущий термин

Задача — зная tid термина, узнать следующий и предыдущий термин.

Решение:

/**
 * Return prev/next term.
 */
function helper_get_sibling_term($tid, $type) {
  $term = taxonomy_term_load($tid);
  if ($type == 'next') {
    $operator = '>';
    $direction = 'ASC';
  }
  else {
    $operator = '<';
    $direction = 'DESC';
  }
  return db_select('taxonomy_term_data', 'td')
    ->fields('td', array('tid', 'name'))
    ->condition('td.vid', $term->vid)
    ->where(
      "td.weight $operator :weight OR (td.weight = :weight AND td.name $operator :name)",
      array(':weight' => $term->weight, ':name' => $term->name)
    )
    ->orderBy('td.weight', $direction)
    ->orderBy('td.name', $direction)
    ->range(0, 1)
    ->execute()
    ->fetchObject();
}

Использование:
$prev_term = helper_get_sibling_term(123, 'prev');
$next_term = helper_get_sibling_term(123, 'next');

Написанное актуально для
Drupal 7
Теги:
работа с бд, таксономия
Добавлено: 30 Июля 2018 08:27:11 Добавил: Андрей Ковальчук Нравится 0
Добавить
Комментарии:
Нету комментариев для вывода...