You are here

public function ContentBuilder::getEntities in Open Social 10.1.x

Same name and namespace in other branches
  1. 8.9 modules/social_features/social_content_block/src/ContentBuilder.php \Drupal\social_content_block\ContentBuilder::getEntities()
  2. 8.8 modules/social_features/social_content_block/src/ContentBuilder.php \Drupal\social_content_block\ContentBuilder::getEntities()
  3. 10.3.x modules/social_features/social_content_block/src/ContentBuilder.php \Drupal\social_content_block\ContentBuilder::getEntities()
  4. 10.0.x modules/social_features/social_content_block/src/ContentBuilder.php \Drupal\social_content_block\ContentBuilder::getEntities()
  5. 10.2.x modules/social_features/social_content_block/src/ContentBuilder.php \Drupal\social_content_block\ContentBuilder::getEntities()

Function to get all the entities based on the filters.

Parameters

string|int $block_id: The block id where we get the settings from.

Return value

array Returns the entities found.

Throws

\Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException

\Drupal\Component\Plugin\Exception\PluginException

\Drupal\Component\Plugin\Exception\PluginNotFoundException

1 call to ContentBuilder::getEntities()
SocialContentBlockLandingPageContentBuilder::getEntities in modules/social_features/social_content_block/modules/social_content_block_landing_page/src/Service/SocialContentBlockLandingPageContentBuilder.php
Function to get all the entities based on the filters.
1 method overrides ContentBuilder::getEntities()
SocialContentBlockLandingPageContentBuilder::getEntities in modules/social_features/social_content_block/modules/social_content_block_landing_page/src/Service/SocialContentBlockLandingPageContentBuilder.php
Function to get all the entities based on the filters.

File

modules/social_features/social_content_block/src/ContentBuilder.php, line 105

Class

ContentBuilder
Class ContentBuilder.

Namespace

Drupal\social_content_block

Code

public function getEntities($block_id) {
  $block_content = $this->entityTypeManager
    ->getStorage('block_content')
    ->load($block_id);
  $plugin_id = $block_content->field_plugin_id->value;
  $definition = $this->contentBlockManager
    ->getDefinition($plugin_id);

  // When the user didn't select any filter in the "Content selection" field
  // then the block base query will be built based on all filled filterable
  // fields.
  if ($block_content->field_plugin_field
    ->isEmpty()) {

    // It could be that the plugin supports more fields than are currently
    // available, those are removed.
    $field_names = array_filter($definition['fields'], static function ($field_name) use ($block_content) {
      return $block_content
        ->hasField($field_name);
    });
  }
  else {
    $field_names = [
      $block_content->field_plugin_field->value,
    ];
  }

  /** @var \Drupal\social_content_block\ContentBlockPluginInterface $plugin */
  $plugin = $this->contentBlockManager
    ->createInstance($plugin_id);

  /** @var \Drupal\Core\Entity\EntityTypeInterface $entity_type */
  $entity_type = $this->entityTypeManager
    ->getDefinition($definition['entityTypeId']);
  $fields = [];
  foreach ($field_names as $field_name) {
    $field = $block_content
      ->get($field_name);

    // Make non-empty entity reference fields easier to use.
    if ($field instanceof EntityReferenceFieldItemListInterface && !$field
      ->isEmpty()) {
      $fields[$field_name] = array_map(function ($item) {
        return $item['target_id'];
      }, $field
        ->getValue());
    }
    elseif (!$field
      ->isEmpty()) {
      $fields[$field_name] = $field
        ->getValue();
    }
  }

  /** @var \Drupal\social_content_block\ContentBlockPluginInterface $plugin */
  $plugin = $this->contentBlockManager
    ->createInstance($plugin_id);

  /** @var \Drupal\Core\Entity\EntityTypeInterface $entity_type */
  $entity_type = $this->entityTypeManager
    ->getDefinition($definition['entityTypeId']);

  /** @var \Drupal\Core\Database\Query\SelectInterface $query */
  $query = $this->connection
    ->select($entity_type
    ->getDataTable(), 'base_table')
    ->fields('base_table', [
    $entity_type
      ->getKey('id'),
  ]);
  if (isset($definition['bundle'])) {
    $query
      ->condition('base_table.' . $entity_type
      ->getKey('bundle'), $definition['bundle']);
  }
  if ($fields) {
    $plugin
      ->query($query, $fields);
  }

  // Allow other modules to change the query to add additions.
  $this->moduleHandler
    ->alter('social_content_block_query', $query, $block_content);

  // Apply our sorting logic.
  $this
    ->sortBy($query, $entity_type, $block_content->field_sorting->value);

  // Add range.
  $query
    ->range(0, $block_content->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($definition['entityTypeId'])
      ->loadMultiple($entities);
    foreach ($entities as $key => $entity) {
      if ($entity
        ->access('view') === FALSE) {
        unset($entities[$key]);
      }
    }
    return $this->entityTypeManager
      ->getViewBuilder($definition['entityTypeId'])
      ->viewMultiple($entities, 'small_teaser');
  }
  return [
    '#markup' => '<div class="card__block">' . $this
      ->t('No matching content found') . '</div>',
    '#prefix' => '<div class="content-list__items">',
    '#suffix' => '</div>',
  ];
}