You are here

public function BynderMediaUsage::bynderMediaInfo in Bynder 8.2

Same name and namespace in other branches
  1. 8.3 src/Controller/BynderMediaUsage.php \Drupal\bynder\Controller\BynderMediaUsage::bynderMediaInfo()
  2. 8 src/Controller/BynderMediaUsage.php \Drupal\bynder\Controller\BynderMediaUsage::bynderMediaInfo()
  3. 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'
bynder.routing.yml in ./bynder.routing.yml
bynder.routing.yml

File

src/Controller/BynderMediaUsage.php, line 77

Class

BynderMediaUsage
Controller contains methods for displaying Bynder media usage info.

Namespace

Drupal\bynder\Controller

Code

public function bynderMediaInfo(Request $request) {

  /** @var \Drupal\node\NodeInterface $node */
  $node = $this
    ->entityTypeManager()
    ->getStorage('node')
    ->load($request->attributes
    ->get('node'));
  $media_types = $this
    ->entityTypeManager()
    ->getStorage('media_type')
    ->loadMultiple();
  $bynder_types = array_filter($media_types, function ($type) {

    /** @var \Drupal\media\MediaTypeInterface $type */
    return $type
      ->getSource() 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 = [];

  /** @var \Drupal\Core\Field\EntityReferenceFieldItemListInterface $field */
  foreach ($entity_reference_fields as $field) {

    /** @var \Drupal\media\Entity\Media $entity */
    foreach ($field
      ->referencedEntities() as $entity) {
      if (in_array($entity
        ->bundle(), array_keys($bynder_types))) {
        $entities[] = $entity;
        $account_domain = $this->configFactory
          ->get('bynder.settings')
          ->get('account_domain');
        $name = $entity
          ->getSource()
          ->getMetadata($entity, 'name');
        $type = $entity
          ->getSource()
          ->getMetadata($entity, 'type');
        $bynder_id = $entity
          ->getSource()
          ->getMetadata($entity, 'uuid');
        $row['name'] = $name ?: $bynder_id;
        $row['type'] = $type ?: $this
          ->t('N/A');
        $row['restriction'] = get_media_restriction($entity
          ->getSource()
          ->getMetadata($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;
}