public function PrevNextApi::add in Previous/Next API 8.2
Create the prev_next records.
Parameters
int $entity_id: Entity id.
string $bundle_name: Entity type.
Overrides PrevNextApiInterface::add
2 calls to PrevNextApi::add()
- PrevNextApi::modifyPointingEntities in src/
PrevNextApi.php - Helper function to update other entities pointing to a particular entity.
- PrevNextApi::update in src/
PrevNextApi.php - Update the prev_next records.
File
- src/
PrevNextApi.php, line 54
Class
- PrevNextApi
- Defines an PrevNextApi service.
Namespace
Drupal\prev_nextCode
public function add($entity_id, $bundle_name) {
$prevnext_bundle = $this->prevnextHelper
->loadBundle($bundle_name);
$search_criteria = $prevnext_bundle
->get('indexing_criteria');
$cond = $this
->bundlesSql($bundle_name, $prevnext_bundle);
if ($search_criteria != 'nid') {
$criteria_value = $this->database
->queryRange("SELECT {$search_criteria} FROM {node_field_data} WHERE nid = :nid", 0, 1, [
':nid' => $entity_id,
])
->fetchField();
$next_nid = $this->database
->queryRange("SELECT nid FROM {node_field_data} WHERE (({$search_criteria} = :value AND nid > :nid) OR {$search_criteria} > :value) AND status = 1 {$cond} ORDER BY {$search_criteria} ASC,nid ASC", 0, 1, [
':value' => $criteria_value,
':nid' => $entity_id,
])
->fetchField();
$prev_nid = $this->database
->queryRange("SELECT nid FROM {node_field_data} WHERE (({$search_criteria} = :value AND nid < :nid) OR {$search_criteria} < :value) AND status = 1 {$cond} ORDER BY {$search_criteria} DESC,nid DESC", 0, 1, [
':value' => $criteria_value,
':nid' => $entity_id,
])
->fetchField();
}
else {
$next_nid = $this->database
->queryRange("SELECT nid FROM {node_field_data} WHERE nid > :nid AND status = 1 {$cond} ORDER BY nid ASC", 0, 1, [
':nid' => $entity_id,
])
->fetchField();
$prev_nid = $this->database
->queryRange("SELECT nid FROM {node_field_data} WHERE nid < :nid AND status = 1 {$cond} ORDER BY nid DESC", 0, 1, [
':nid' => $entity_id,
])
->fetchField();
}
// Update the node-level data.
$exists = (bool) $this->database
->queryRange('SELECT 1 FROM {prev_next_node} WHERE nid = :nid', 0, 1, [
':nid' => $entity_id,
])
->fetchField();
if (!empty($exists)) {
$this->database
->update('prev_next_node')
->fields([
'prev_nid' => $prev_nid ? $prev_nid : 0,
'next_nid' => $next_nid ? $next_nid : 0,
'changed' => REQUEST_TIME,
])
->condition('nid', $entity_id)
->execute();
}
else {
$id = $this->database
->insert('prev_next_node')
->fields([
'prev_nid' => $prev_nid ? $prev_nid : 0,
'next_nid' => $next_nid ? $next_nid : 0,
'changed' => REQUEST_TIME,
'nid' => $entity_id,
])
->execute();
}
// Update the other entities pointing to this entity.
foreach (NodeType::loadMultiple() as $type => $name) {
$prevnext_bundles = $this->prevnextHelper
->getBundleNames();
if (in_array($type, $prevnext_bundles)) {
$prevnext_bundle = $this->prevnextHelper
->loadBundle($type);
$search_criteria = $prevnext_bundle
->get('indexing_criteria');
$cond = $this
->bundlesSql($bundle_name, $prevnext_bundle);
if ($search_criteria != 'nid') {
$criteria_value = $this->database
->queryRange("SELECT {$search_criteria} FROM {node_field_data} WHERE nid = :nid", 0, 1, [
':nid' => $entity_id,
])
->fetchField();
$prev_nid = $this->database
->queryRange("SELECT nid FROM {node_field_data} WHERE (({$search_criteria} = :value AND nid > :nid) OR {$search_criteria} > :value) AND status = 1 {$cond} ORDER BY {$search_criteria} ASC,nid ASC", 0, 1, [
':value' => $criteria_value,
':nid' => $entity_id,
])
->fetchField();
$next_nid = $this->database
->queryRange("SELECT nid FROM {node_field_data} WHERE (({$search_criteria} = :value AND nid < :nid) OR {$search_criteria} < :value) AND status = 1 {$cond} ORDER BY {$search_criteria} DESC,nid DESC", 0, 1, [
':value' => $criteria_value,
':nid' => $entity_id,
])
->fetchField();
}
else {
$prev_nid = $this->database
->queryRange("SELECT nid FROM {node_field_data} WHERE nid > :nid AND status = 1 {$cond} ORDER BY nid ASC", 0, 1, [
':nid' => $entity_id,
])
->fetchField();
$next_nid = $this->database
->queryRange("SELECT nid FROM {node_field_data} WHERE nid < :nid AND status = 1 {$cond} ORDER BY nid DESC", 0, 1, [
':nid' => $entity_id,
])
->fetchField();
}
}
if ($next_nid) {
$this->database
->update('prev_next_node')
->fields([
'next_nid' => $entity_id,
])
->condition('nid', $next_nid)
->execute();
}
if ($prev_nid) {
$this->database
->update('prev_next_node')
->fields([
'prev_nid' => $entity_id,
])
->condition('nid', $prev_nid)
->execute();
}
}
}