You are here

protected function RecentGroupContentBlock::getGroupContent in Organic groups 8

Returns the most recent group content for the active group.

Return value

\Drupal\Core\Entity\EntityInterface[] The most recent group content for the group which is currently active according to OgContext.

1 call to RecentGroupContentBlock::getGroupContent()
RecentGroupContentBlock::build in src/Plugin/Block/RecentGroupContentBlock.php
Builds and returns the renderable array for this block plugin.

File

src/Plugin/Block/RecentGroupContentBlock.php, line 234

Class

RecentGroupContentBlock
Provides a block that shows recent group content for the current group.

Namespace

Drupal\og\Plugin\Block

Code

protected function getGroupContent() {
  $group = $this->ogContext
    ->getGroup();
  $entity_type = $this->configuration['entity_type'];
  $bundles = array_filter($this->configuration['bundles'][$entity_type]);
  $definition = $this->entityTypeManager
    ->getDefinition($entity_type);

  // Retrieve the fields which reference our entity type and bundle.
  $field_storage_config_storage = $this->entityTypeManager
    ->getStorage('field_storage_config');
  $query = $field_storage_config_storage
    ->getQuery()
    ->condition('type', OgGroupAudienceHelperInterface::GROUP_REFERENCE)
    ->condition('entity_type', $entity_type);

  /** @var \Drupal\field\FieldStorageConfigInterface[] $fields */
  $fields = array_filter($field_storage_config_storage
    ->loadMultiple($query
    ->execute()), function (FieldStorageConfigInterface $field) use ($group) {
    $type_matches = $field
      ->getSetting('target_type') === $group
      ->getEntityTypeId();

    // If the list of target bundles is empty, it targets all bundles.
    $bundle_matches = empty($field
      ->getSetting('target_bundles')) || in_array($group
      ->bundle(), $field
      ->getSetting('target_bundles'));
    return $type_matches && $bundle_matches;
  });

  // Compile the group content.
  $ids = [];
  foreach ($fields as $field) {

    // Query all group content that references the group through this field.
    $results = $this->entityTypeManager
      ->getStorage($entity_type)
      ->getQuery()
      ->condition($field
      ->getName() . '.target_id', $group
      ->id())
      ->condition($definition
      ->getKey('bundle'), $bundles, 'IN')
      ->accessCheck()
      ->sort('created', 'DESC')
      ->range(0, $this->configuration['count'])
      ->execute();
    $ids = array_merge($ids, $results);
  }
  return $this->entityTypeManager
    ->getStorage($entity_type)
    ->loadMultiple($ids);
}