You are here

class SocialActivityLazyBuilder in Open Social 10.1.x

Same name and namespace in other branches
  1. 8.9 modules/social_features/social_activity/src/SocialActivityLazyBuilder.php \Drupal\social_activity\SocialActivityLazyBuilder
  2. 10.3.x modules/social_features/social_activity/src/SocialActivityLazyBuilder.php \Drupal\social_activity\SocialActivityLazyBuilder
  3. 10.0.x modules/social_features/social_activity/src/SocialActivityLazyBuilder.php \Drupal\social_activity\SocialActivityLazyBuilder
  4. 10.2.x modules/social_features/social_activity/src/SocialActivityLazyBuilder.php \Drupal\social_activity\SocialActivityLazyBuilder

Class SocialActivityLazyBuilder.

@package Drupal\social_activity

Hierarchy

Expanded class hierarchy of SocialActivityLazyBuilder

1 string reference to 'SocialActivityLazyBuilder'
social_activity.services.yml in modules/social_features/social_activity/social_activity.services.yml
modules/social_features/social_activity/social_activity.services.yml
1 service uses SocialActivityLazyBuilder
social_activity.lazy_builder in modules/social_features/social_activity/social_activity.services.yml
Drupal\social_activity\SocialActivityLazyBuilder

File

modules/social_features/social_activity/src/SocialActivityLazyBuilder.php, line 14

Namespace

Drupal\social_activity
View source
class SocialActivityLazyBuilder implements TrustedCallbackInterface {

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

  /**
   * The views executable factory.
   *
   * @var \Drupal\views\ViewExecutableFactory
   */
  protected $viewExecutable;

  /**
   * SocialActivityLazyBuilder constructor.
   *
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   * @param \Drupal\views\ViewExecutableFactory $view_executable
   *   The views executable factory.
   */
  public function __construct(EntityTypeManagerInterface $entity_type_manager, ViewExecutableFactory $view_executable) {
    $this->entityTypeManager = $entity_type_manager;
    $this->viewExecutable = $view_executable;
  }

  /**
   * {@inheritdoc}
   */
  public static function trustedCallbacks() {
    return [
      'viewsLazyBuild',
    ];
  }

  /**
   * Returns views render for lazy builder.
   *
   * @param string $view_id
   *   The views ID.
   * @param string $display_id
   *   The views display ID.
   * @param string $node_type
   *   Node bundle.
   * @param int $item_per_page
   *   Items to display.
   * @param string|null $vocabulary
   *   Vocabulary ID.
   * @param mixed $tags
   *   List of tags IDs.
   *
   * @return array|null
   *   Render array.
   */
  public function viewsLazyBuild($view_id, $display_id, $node_type, $item_per_page, $vocabulary = NULL, ...$tags) {

    // Get view.
    $view_entity = $this->entityTypeManager
      ->getStorage('view')
      ->load($view_id);
    $view = $this->viewExecutable
      ->get($view_entity);
    $view
      ->setDisplay($display_id);
    $view
      ->setItemsPerPage($item_per_page);

    // Add filtration if tags and vocabulary exists.
    if ($vocabulary && $vocabulary !== '_none' && !empty($tags)) {
      $view->filter_tags = $tags;
      $view->filter_vocabulary = $vocabulary;
    }
    $view
      ->preExecute();
    $view
      ->execute($display_id);

    // Change entity display and add attachments if views block in dashboard.
    if ($view
      ->id() == "activity_stream" && $node_type === 'dashboard') {
      $view->rowPlugin->options['view_mode'] = 'featured';
      $view->element['#attached']['library'][] = 'social_featured_content/paragraph.featured';
    }

    // Get views content.
    $content = $view
      ->render($display_id);
    return $content;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
SocialActivityLazyBuilder::$entityTypeManager protected property The entity type manager.
SocialActivityLazyBuilder::$viewExecutable protected property The views executable factory.
SocialActivityLazyBuilder::trustedCallbacks public static function Lists the trusted callbacks provided by the implementing class. Overrides TrustedCallbackInterface::trustedCallbacks
SocialActivityLazyBuilder::viewsLazyBuild public function Returns views render for lazy builder.
SocialActivityLazyBuilder::__construct public function SocialActivityLazyBuilder constructor.
TrustedCallbackInterface::THROW_EXCEPTION constant Untrusted callbacks throw exceptions.
TrustedCallbackInterface::TRIGGER_SILENCED_DEPRECATION constant Untrusted callbacks trigger silenced E_USER_DEPRECATION errors.
TrustedCallbackInterface::TRIGGER_WARNING constant Untrusted callbacks trigger E_USER_WARNING errors.