public function BynderMediaUsage::bynderMediaInfo in Bynder 8
Same name and namespace in other branches
- 8.3 src/Controller/BynderMediaUsage.php \Drupal\bynder\Controller\BynderMediaUsage::bynderMediaInfo()
- 8.2 src/Controller/BynderMediaUsage.php \Drupal\bynder\Controller\BynderMediaUsage::bynderMediaInfo()
- 4.0.x src/Controller/BynderMediaUsage.php \Drupal\bynder\Controller\BynderMediaUsage::bynderMediaInfo()
The Bynder media usage info.
Parameters
\Symfony\Component\HttpFoundation\Request $request: Represents an HTTP request.
Return value
mixed Bynder media usage list.
1 string reference to 'BynderMediaUsage::bynderMediaInfo'
File
- src/
Controller/ BynderMediaUsage.php, line 77
Class
- BynderMediaUsage
- Controller contains methods for displaying Bynder media usage info.
Namespace
Drupal\bynder\ControllerCode
public function bynderMediaInfo(Request $request) {
$node = $this
->entityTypeManager()
->getStorage('node')
->load($request->attributes
->get('node'));
$media_bundles = $this
->entityTypeManager()
->getStorage('media_bundle')
->loadMultiple();
$bynder_bundles = array_filter($media_bundles, function ($bundle) {
return $bundle
->getType() instanceof Bynder;
});
$entity_reference_fields = array_filter($node
->getFields(), function ($field) {
return $field instanceof EntityReferenceFieldItemList;
});
$header = [
[
'data' => $this
->t('Name'),
],
[
'data' => $this
->t('Type'),
],
[
'data' => $this
->t('Usage Restriction'),
],
[
'data' => $this
->t('Action'),
],
];
$rows = [];
$entities = [];
foreach ($entity_reference_fields as $field) {
/** @var \Drupal\media_entity\Entity\Media $entity */
foreach ($field
->referencedEntities() as $entity) {
if (in_array($entity
->bundle(), array_keys($bynder_bundles))) {
$entities[] = $entity;
$account_domain = $this->configFactory
->get('bynder.settings')
->get('account_domain');
$name = $entity
->getType()
->getField($entity, 'name');
$type = $entity
->getType()
->getField($entity, 'type');
$bynder_id = $entity
->getType()
->getField($entity, 'uuid');
$row['name'] = $name ?: $bynder_id;
$row['type'] = $type ?: $this
->t('N/A');
$row['restriction'] = get_media_restriction($entity
->getType()
->getField($entity, 'propertyOptions'));
$links['edit'] = [
'title' => $this
->t('Edit'),
'url' => $entity
->toUrl('edit-form'),
];
$links['edit_on_bynder'] = [
'title' => $this
->t('Edit on Bynder'),
'url' => Url::fromUri($account_domain . '/media', [
'query' => [
'mediaId' => $bynder_id,
],
]),
];
$row['actions']['data'] = [
'#type' => 'operations',
'#links' => $links,
];
$rows[] = [
'data' => $row,
];
}
}
}
$table = [
'#theme' => 'table',
'#header' => $header,
'#rows' => $rows,
'#empty' => $this
->t("There are no Bynder media found on the page."),
];
$this->renderer
->addCacheableDependency($table, $node);
$this->renderer
->addCacheableDependency($table, $this->configFactory
->get('bynder.settings'));
foreach ($entities as $entity) {
$this->renderer
->addCacheableDependency($table, $entity);
}
return $table;
}