protected function ContentBuilder::getTopics in Open Social 8.7
Same name and namespace in other branches
- 8.6 modules/social_features/social_content_block/src/ContentBuilder.php \Drupal\social_content_block\ContentBuilder::getTopics()
Function to get all the topics based on the filters.
Parameters
\Drupal\block_content\Entity\BlockContent $block_content: The block content where we get the settings from.
Return value
array|string Return the topics found.
Throws
\Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
\Drupal\Component\Plugin\Exception\PluginNotFoundException
1 call to ContentBuilder::getTopics()
- ContentBuilder::build in modules/
social_features/ social_content_block/ src/ ContentBuilder.php - Lazy builder callback for displaying a content blocks.
File
- modules/
social_features/ social_content_block/ src/ ContentBuilder.php, line 81
Class
- ContentBuilder
- Class ContentBuilder.
Namespace
Drupal\social_content_blockCode
protected function getTopics(BlockContent $block_content) {
// Get topic type tags.
$topic_types_list = $block_content
->get('field_topic_type')
->getValue();
$topic_types = array_map(function ($topic_type) {
return $topic_type['target_id'];
}, $topic_types_list);
// Get group tags.
$group_tag_list = $block_content
->get('field_group')
->getValue();
$group_tags = array_map(function ($group_tag) {
return $group_tag['target_id'];
}, $group_tag_list);
// Get social tags.
$social_tag_list = $block_content
->get('field_content_tags')
->getValue();
$social_tags = array_map(function ($social_tag) {
return $social_tag['target_id'];
}, $social_tag_list);
// Use database select because we need joins
// which are not possible with entityQuery.
/** @var \Drupal\Core\Database\Query\SelectInterface $query */
$query = $this->connection
->select('node_field_data', 'n')
->fields('n', [
'nid',
])
->condition('n.type', 'topic');
// Add topic type tags.
if (!empty($topic_types)) {
$query
->innerJoin('node__field_topic_type', 'tt', 'tt.entity_id = n.nid');
$query
->condition('tt.field_topic_type_target_id', $topic_types, 'IN');
}
// Add group tags.
if (!empty($group_tags)) {
$query
->innerJoin('group_content_field_data', 'gd', 'gd.entity_id = n.nid');
$query
->condition('gd.gid', $group_tags, 'IN');
}
if (!empty($social_tags)) {
$query
->innerJoin('node__social_tagging', 'st', 'st.entity_id = n.nid');
$query
->condition('st.social_tagging_target_id', $social_tags, 'IN');
}
// Allow other modules to change the query to add additions.
$this->moduleHandler
->alter('social_content_block_query', $query, $block_content);
// Add sorting.
$query
->orderBy('n.' . $block_content
->getFieldValue('field_sorting', 'value'));
// Add range.
$query
->range(0, $block_content
->getFieldValue('field_item_amount', 'value'));
// Execute the query to get the results.
$entities = $query
->execute()
->fetchAllKeyed(0, 0);
if ($entities) {
// Load all the topics so we can give them back.
$entities = $this->entityTypeManager
->getStorage('node')
->loadMultiple($entities);
return $this->entityTypeManager
->getViewBuilder('node')
->viewMultiple($entities, 'small_teaser');
}
return [
'#markup' => '<div class="card__block">' . $this
->t('No matching content found') . '</div>',
];
}