class WorkspaceViewBuilder

Same name in other branches
  1. 9 core/modules/workspaces/src/WorkspaceViewBuilder.php \Drupal\workspaces\WorkspaceViewBuilder
  2. 11.x core/modules/workspaces/src/WorkspaceViewBuilder.php \Drupal\workspaces\WorkspaceViewBuilder

Provides a Workspace view builder.

Hierarchy

Expanded class hierarchy of WorkspaceViewBuilder

File

core/modules/workspaces/src/WorkspaceViewBuilder.php, line 15

Namespace

Drupal\workspaces
View source
class WorkspaceViewBuilder extends EntityViewBuilder {
    
    /**
     * The entity type manager.
     *
     * @var \Drupal\Core\Entity\EntityTypeManagerInterface
     */
    protected $entityTypeManager;
    
    /**
     * The workspace association service.
     *
     * @var \Drupal\workspaces\WorkspaceAssociationInterface
     */
    protected $workspaceAssociation;
    
    /**
     * The date formatter service.
     *
     * @var \Drupal\Core\Datetime\DateFormatterInterface
     */
    protected $dateFormatter;
    
    /**
     * The entity bundle information service.
     *
     * @var \Drupal\Core\Entity\EntityTypeBundleInfoInterface
     */
    protected $bundleInfo;
    
    /**
     * The number of entities to display on the workspace manage page.
     */
    protected int|false $limit = 50;
    
    /**
     * {@inheritdoc}
     */
    public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
        $instance = parent::createInstance($container, $entity_type);
        $instance->entityTypeManager = $container->get('entity_type.manager');
        $instance->workspaceAssociation = $container->get('workspaces.association');
        $instance->dateFormatter = $container->get('date.formatter');
        $instance->bundleInfo = $container->get('entity_type.bundle.info');
        return $instance;
    }
    
    /**
     * {@inheritdoc}
     */
    public function buildComponents(array &$build, array $entities, array $displays, $view_mode) {
        parent::buildComponents($build, $entities, $displays, $view_mode);
        $bundle_info = $this->bundleInfo
            ->getAllBundleInfo();
        $header = [
            'title' => $this->t('Title'),
            'type' => $this->t('Type'),
            'changed' => $this->t('Last changed'),
            'owner' => $this->t('Author'),
            'operations' => $this->t('Operations'),
        ];
        foreach ($entities as $build_id => $entity) {
            // Display the number of entities changed in the workspace regardless of
            // how many of them are listed on each page.
            $changes_count = [];
            $all_tracked_entities = $this->workspaceAssociation
                ->getTrackedEntities($entity->id());
            foreach ($all_tracked_entities as $entity_type_id => $tracked_entity_ids) {
                $changes_count[$entity_type_id] = $this->entityTypeManager
                    ->getDefinition($entity_type_id)
                    ->getCountLabel(count($tracked_entity_ids));
            }
            $build[$build_id]['changes']['overview'] = [
                '#type' => 'item',
                '#title' => $this->t('Workspace changes'),
            ];
            $build[$build_id]['changes']['list'] = [
                '#type' => 'table',
                '#header' => $header,
                '#empty' => $this->t('This workspace has no changes.'),
            ];
            $paged_tracked_entities = $this->workspaceAssociation
                ->getTrackedEntitiesForListing($entity->id(), $build_id, $this->limit);
            foreach ($paged_tracked_entities as $entity_type_id => $tracked_entities) {
                $entity_type = $this->entityTypeManager
                    ->getDefinition($entity_type_id);
                if ($this->entityTypeManager
                    ->hasHandler($entity_type_id, 'list_builder')) {
                    $list_builder = $this->entityTypeManager
                        ->getListBuilder($entity_type_id);
                }
                else {
                    $list_builder = $this->entityTypeManager
                        ->createHandlerInstance(EntityListBuilder::class, $entity_type);
                }
                $revisions = $this->entityTypeManager
                    ->getStorage($entity_type_id)
                    ->loadMultipleRevisions(array_keys($tracked_entities));
                // Load all users at once.
                $user_ids = [];
                foreach ($revisions as $revision) {
                    if ($revision instanceof EntityOwnerInterface) {
                        $user_ids[$revision->getOwnerId()] = $revision->getOwnerId();
                    }
                }
                if ($user_ids = array_filter($user_ids)) {
                    $revision_owners = $this->entityTypeManager
                        ->getStorage('user')
                        ->loadMultiple($user_ids);
                }
                foreach ($revisions as $revision) {
                    if ($revision->getEntityType()
                        ->hasLinkTemplate('canonical')) {
                        $title = [
                            '#type' => 'link',
                            '#title' => $revision->label(),
                            '#url' => $revision->toUrl(),
                        ];
                    }
                    else {
                        $title = [
                            '#markup' => $revision->label(),
                        ];
                    }
                    if (count($bundle_info[$entity_type_id]) > 1) {
                        $type = [
                            '#markup' => $this->t('@entity_type_label: @entity_bundle_label', [
                                '@entity_type_label' => $entity_type->getLabel(),
                                '@entity_bundle_label' => $bundle_info[$entity_type_id][$revision->bundle()]['label'],
                            ]),
                        ];
                    }
                    else {
                        $type = [
                            '#markup' => $bundle_info[$entity_type_id][$revision->bundle()]['label'],
                        ];
                    }
                    $changed = $revision instanceof EntityChangedInterface ? $this->dateFormatter
                        ->format($revision->getChangedTime()) : '';
                    if ($revision instanceof EntityOwnerInterface && isset($revision_owners[$revision->getOwnerId()])) {
                        $author = [
                            '#theme' => 'username',
                            '#account' => $revision_owners[$revision->getOwnerId()],
                        ];
                    }
                    else {
                        $author = [
                            '#markup' => '',
                        ];
                    }
                    $build[$build_id]['changes']['list'][$entity_type_id . ':' . $revision->id()] = [
                        '#entity' => $revision,
                        'title' => $title,
                        'type' => $type,
                        'changed' => [
                            '#markup' => $changed,
                        ],
                        'owner' => $author,
                        'operations' => [
                            '#type' => 'operations',
                            '#links' => $list_builder->getOperations($revision),
                        ],
                    ];
                }
            }
            if ($changes_count) {
                $build[$build_id]['changes']['overview']['#markup'] = implode(', ', $changes_count);
            }
            $build[$build_id]['pager'] = [
                '#type' => 'pager',
                '#element' => $build_id,
            ];
        }
    }

}

Members

Title Sort descending Deprecated Modifiers Object type Summary Overriden Title Overrides
DependencySerializationTrait::$_entityStorages protected property
DependencySerializationTrait::$_serviceIds protected property
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
EntityHandlerBase::$moduleHandler protected property The module handler to invoke hooks on. 5
EntityHandlerBase::moduleHandler protected function Gets the module handler. 5
EntityHandlerBase::setModuleHandler public function Sets the module handler for this handler.
EntityViewBuilder::$cacheBin protected property The cache bin used to store the render cache.
EntityViewBuilder::$entityDisplayRepository protected property The entity display repository.
EntityViewBuilder::$entityRepository protected property The entity repository service.
EntityViewBuilder::$entityType protected property Information about the entity type.
EntityViewBuilder::$entityTypeId protected property The type of entities for which this view builder is instantiated.
EntityViewBuilder::$languageManager protected property The language manager.
EntityViewBuilder::$singleFieldDisplays protected property The EntityViewDisplay objects created for individual field rendering.
EntityViewBuilder::$themeRegistry protected property The theme registry.
EntityViewBuilder::addContextualLinks protected function Add contextual links.
EntityViewBuilder::alterBuild protected function Specific per-entity building. 1
EntityViewBuilder::build public function Builds an entity's view; augments entity defaults.
EntityViewBuilder::buildMultiple public function Builds multiple entities' views; augments entity defaults.
EntityViewBuilder::getBuildDefaults protected function Provides entity-specific defaults to the build process. 4
EntityViewBuilder::getCacheTags public function The cache tag associated with this entity view builder. Overrides EntityViewBuilderInterface::getCacheTags
EntityViewBuilder::getSingleFieldDisplay protected function Gets an EntityViewDisplay for rendering an individual field.
EntityViewBuilder::isViewModeCacheable protected function Determines whether the view mode is cacheable.
EntityViewBuilder::resetCache public function Resets the entity render cache. Overrides EntityViewBuilderInterface::resetCache
EntityViewBuilder::trustedCallbacks public static function Lists the trusted callbacks provided by the implementing class. Overrides TrustedCallbackInterface::trustedCallbacks 2
EntityViewBuilder::view public function Builds the render array for the provided entity. Overrides EntityViewBuilderInterface::view 4
EntityViewBuilder::viewField public function Builds a renderable array for the value of a single field in an entity. Overrides EntityViewBuilderInterface::viewField
EntityViewBuilder::viewFieldItem public function Builds a renderable array for a single field item. Overrides EntityViewBuilderInterface::viewFieldItem
EntityViewBuilder::viewMultiple public function Builds the render array for the provided entities. Overrides EntityViewBuilderInterface::viewMultiple 4
EntityViewBuilder::__construct public function Constructs a new EntityViewBuilder. 1
StringTranslationTrait::$stringTranslation protected property The string translation service. 3
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.
TrustedCallbackInterface::THROW_EXCEPTION constant Untrusted callbacks throw exceptions.
TrustedCallbackInterface::TRIGGER_SILENCED_DEPRECATION constant Untrusted callbacks trigger silenced E_USER_DEPRECATION errors.
TrustedCallbackInterface::TRIGGER_WARNING Deprecated constant Untrusted callbacks trigger E_USER_WARNING errors.
WorkspaceViewBuilder::$bundleInfo protected property The entity bundle information service.
WorkspaceViewBuilder::$dateFormatter protected property The date formatter service.
WorkspaceViewBuilder::$entityTypeManager protected property The entity type manager.
WorkspaceViewBuilder::$limit protected property The number of entities to display on the workspace manage page.
WorkspaceViewBuilder::$workspaceAssociation protected property The workspace association service.
WorkspaceViewBuilder::buildComponents public function Builds the component fields and properties of a set of entities. Overrides EntityViewBuilder::buildComponents
WorkspaceViewBuilder::createInstance public static function Instantiates a new instance of this entity handler. Overrides EntityViewBuilder::createInstance

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.