View source
<?php
namespace Drupal\bynder\Controller;
use Drupal\bynder\BynderApiInterface;
use Drupal\bynder\Plugin\MediaEntity\Type\Bynder;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Field\EntityReferenceFieldItemList;
use Drupal\Core\Render\RendererInterface;
use Drupal\Core\Url;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;
class BynderMediaUsage extends ControllerBase {
protected $bynder;
protected $configFactory;
protected $renderer;
public function __construct(BynderApiInterface $bynder, ConfigFactoryInterface $config_factory, RendererInterface $renderer_object) {
$this->bynder = $bynder;
$this->configFactory = $config_factory;
$this->renderer = $renderer_object;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('bynder_api'), $container
->get('config.factory'), $container
->get('renderer'));
}
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) {
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;
}
}