You are here

class ContentBuilder in Open Social 8.6

Same name and namespace in other branches
  1. 8.9 modules/social_features/social_content_block/src/ContentBuilder.php \Drupal\social_content_block\ContentBuilder
  2. 8.7 modules/social_features/social_content_block/src/ContentBuilder.php \Drupal\social_content_block\ContentBuilder
  3. 8.8 modules/social_features/social_content_block/src/ContentBuilder.php \Drupal\social_content_block\ContentBuilder
  4. 10.3.x modules/social_features/social_content_block/src/ContentBuilder.php \Drupal\social_content_block\ContentBuilder
  5. 10.0.x modules/social_features/social_content_block/src/ContentBuilder.php \Drupal\social_content_block\ContentBuilder
  6. 10.1.x modules/social_features/social_content_block/src/ContentBuilder.php \Drupal\social_content_block\ContentBuilder
  7. 10.2.x modules/social_features/social_content_block/src/ContentBuilder.php \Drupal\social_content_block\ContentBuilder

Class ContentBuilder.

@package Drupal\social_content_block

Hierarchy

Expanded class hierarchy of ContentBuilder

1 string reference to 'ContentBuilder'
social_content_block.services.yml in modules/social_features/social_content_block/social_content_block.services.yml
modules/social_features/social_content_block/social_content_block.services.yml
1 service uses ContentBuilder
social_content_block.content_builder in modules/social_features/social_content_block/social_content_block.services.yml
Drupal\social_content_block\ContentBuilder

File

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

Namespace

Drupal\social_content_block
View source
class ContentBuilder implements ContentBuilderInterface {
  use StringTranslationTrait;

  /**
   * The entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * The module handler service.
   *
   * @var \Drupal\Core\Extension\ModuleHandlerInterface
   */
  protected $moduleHandler;

  /**
   * The module handler service.
   *
   * @var \Drupal\Core\Database\Connection
   */
  protected $connection;

  /**
   * ContentBuilder constructor.
   *
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   * @param \Drupal\Core\Database\Connection $connection
   *   Database Service Object.
   * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
   *   The module handler to invoke the alter hook with.
   * @param \Drupal\Core\StringTranslation\TranslationInterface $string_translation
   *   The string translation.
   */
  public function __construct(EntityTypeManagerInterface $entity_type_manager, Connection $connection, ModuleHandlerInterface $module_handler, TranslationInterface $string_translation) {
    $this->entityTypeManager = $entity_type_manager;
    $this->connection = $connection;
    $this->moduleHandler = $module_handler;
    $this
      ->setStringTranslation($string_translation);
  }

  /**
   * Function to get all the topics based on the filters.
   *
   * @param \Drupal\block_content\Entity\BlockContent $block_content
   *   The block content where we get the settings from.
   *
   * @return array|string
   *   Return the topics found.
   *
   * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
   * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
   */
  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>',
    ];
  }

  /**
   * Function to generate the read more link.
   *
   * @param \Drupal\block_content\Entity\BlockContent $block_content
   *   The block content where we get the settings from.
   *
   * @return string
   *   The read more link.
   */
  protected function getLink(BlockContent $block_content) {
    $field = $block_content->field_link;
    if (!$field
      ->isEmpty()) {
      $url = Url::fromUri($field->uri);
      $link_options = [
        'attributes' => [
          'class' => [
            'btn',
            'btn-flat',
          ],
        ],
      ];
      $url
        ->setOptions($link_options);
      return Link::fromTextAndUrl($field->title, $url)
        ->toString();
    }
    return '';
  }

  /**
   * {@inheritdoc}
   */
  public function build($entity_type_id, $entity_id) {
    if ($entity_type_id !== 'block_content') {
      return [];
    }
    $block_content = $this->entityTypeManager
      ->getStorage('block_content')
      ->load($entity_id);
    if (!$block_content instanceof BlockContentInterface || $block_content
      ->bundle() !== 'custom_content_list') {
      return [];
    }
    $data = [
      '#theme' => 'social_content_block',
      '#title' => $block_content
        ->label(),
      '#subtitle' => $block_content
        ->getFieldValue('field_subtitle', 'value'),
      '#topics' => $this
        ->getTopics($block_content),
      '#link' => $this
        ->getLink($block_content),
    ];
    $build['content'] = $data;
    return $build;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ContentBuilder::$connection protected property The module handler service.
ContentBuilder::$entityTypeManager protected property The entity type manager.
ContentBuilder::$moduleHandler protected property The module handler service.
ContentBuilder::build public function Lazy builder callback for displaying a content blocks. Overrides ContentBuilderInterface::build
ContentBuilder::getLink protected function Function to generate the read more link.
ContentBuilder::getTopics protected function Function to get all the topics based on the filters.
ContentBuilder::__construct public function ContentBuilder constructor.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.