class WorkspaceViewBuilder in Drupal 9
Provides a Workspace view builder.
Hierarchy
- class \Drupal\Core\Entity\EntityHandlerBase uses DependencySerializationTrait, StringTranslationTrait
- class \Drupal\Core\Entity\EntityViewBuilder implements EntityHandlerInterface, EntityViewBuilderInterface, TrustedCallbackInterface
- class \Drupal\workspaces\WorkspaceViewBuilder
- class \Drupal\Core\Entity\EntityViewBuilder implements EntityHandlerInterface, EntityViewBuilderInterface, TrustedCallbackInterface
Expanded class hierarchy of WorkspaceViewBuilder
File
- core/
modules/ workspaces/ src/ WorkspaceViewBuilder.php, line 15
Namespace
Drupal\workspacesView 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;
/**
* {@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) {
$all_tracked_entities = $this->workspaceAssociation
->getTrackedEntities($entity
->id());
$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.'),
];
$changes_count = [];
foreach ($all_tracked_entities as $entity_type_id => $tracked_entities) {
// Ensure that newest revisions are displayed at the top.
krsort($tracked_entities);
$changes_count[$entity_type_id] = $this->entityTypeManager
->getDefinition($entity_type_id)
->getCountLabel(count($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);
}
}
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DependencySerializationTrait:: |
protected | property | ||
DependencySerializationTrait:: |
protected | property | ||
DependencySerializationTrait:: |
public | function | 2 | |
DependencySerializationTrait:: |
public | function | 2 | |
EntityHandlerBase:: |
protected | property | The module handler to invoke hooks on. | 5 |
EntityHandlerBase:: |
protected | function | Gets the module handler. | 5 |
EntityHandlerBase:: |
public | function | Sets the module handler for this handler. | |
EntityViewBuilder:: |
protected | property | The cache bin used to store the render cache. | |
EntityViewBuilder:: |
protected | property | The entity display repository. | |
EntityViewBuilder:: |
protected | property | The entity repository service. | |
EntityViewBuilder:: |
protected | property | Information about the entity type. | |
EntityViewBuilder:: |
protected | property | The type of entities for which this view builder is instantiated. | |
EntityViewBuilder:: |
protected | property | The language manager. | |
EntityViewBuilder:: |
protected | property | The EntityViewDisplay objects created for individual field rendering. | |
EntityViewBuilder:: |
protected | property | The theme registry. | |
EntityViewBuilder:: |
protected | function | Add contextual links. | |
EntityViewBuilder:: |
protected | function | Specific per-entity building. | 1 |
EntityViewBuilder:: |
public | function | Builds an entity's view; augments entity defaults. | |
EntityViewBuilder:: |
public | function | Builds multiple entities' views; augments entity defaults. | |
EntityViewBuilder:: |
protected | function | Provides entity-specific defaults to the build process. | 4 |
EntityViewBuilder:: |
public | function |
The cache tag associated with this entity view builder. Overrides EntityViewBuilderInterface:: |
|
EntityViewBuilder:: |
protected | function | Gets an EntityViewDisplay for rendering an individual field. | |
EntityViewBuilder:: |
protected | function | Determines whether the view mode is cacheable. | |
EntityViewBuilder:: |
public | function |
Resets the entity render cache. Overrides EntityViewBuilderInterface:: |
|
EntityViewBuilder:: |
public static | function |
Lists the trusted callbacks provided by the implementing class. Overrides TrustedCallbackInterface:: |
2 |
EntityViewBuilder:: |
public | function |
Builds the render array for the provided entity. Overrides EntityViewBuilderInterface:: |
4 |
EntityViewBuilder:: |
public | function |
Builds a renderable array for the value of a single field in an entity. Overrides EntityViewBuilderInterface:: |
|
EntityViewBuilder:: |
public | function |
Builds a renderable array for a single field item. Overrides EntityViewBuilderInterface:: |
|
EntityViewBuilder:: |
public | function |
Builds the render array for the provided entities. Overrides EntityViewBuilderInterface:: |
4 |
EntityViewBuilder:: |
public | function | Constructs a new EntityViewBuilder. | 2 |
StringTranslationTrait:: |
protected | property | The string translation service. | 4 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. | |
TrustedCallbackInterface:: |
constant | Untrusted callbacks throw exceptions. | ||
TrustedCallbackInterface:: |
constant | Untrusted callbacks trigger silenced E_USER_DEPRECATION errors. | ||
TrustedCallbackInterface:: |
constant | Untrusted callbacks trigger E_USER_WARNING errors. | ||
WorkspaceViewBuilder:: |
protected | property | The entity bundle information service. | |
WorkspaceViewBuilder:: |
protected | property | The date formatter service. | |
WorkspaceViewBuilder:: |
protected | property | The entity type manager. | |
WorkspaceViewBuilder:: |
protected | property | The workspace association service. | |
WorkspaceViewBuilder:: |
public | function |
Builds the component fields and properties of a set of entities. Overrides EntityViewBuilder:: |
|
WorkspaceViewBuilder:: |
public static | function |
Instantiates a new instance of this entity handler. Overrides EntityViewBuilder:: |