You are here

function brightcove_field_refresh_video_entity_link in Brightcove Video Connect 7.7

Same name and namespace in other branches
  1. 7.2 brightcove_field/brightcove_field.module \brightcove_field_refresh_video_entity_link()
  2. 7.3 brightcove_field/brightcove_field.module \brightcove_field_refresh_video_entity_link()
  3. 7.4 brightcove_field/brightcove_field.module \brightcove_field_refresh_video_entity_link()
  4. 7.5 brightcove_field/brightcove_field.module \brightcove_field_refresh_video_entity_link()
  5. 7.6 brightcove.module \brightcove_field_refresh_video_entity_link()

Updates the video entity link on a video.

Parameters

$entity_type: Type of the entity to reference.

$entity: The entity to reference.

$items: The $items array of hook_field_insert() and hook_field_update(). Example: array( array('video_id' => 0), array('video_id' => 1), array('video_id' => 2), )

2 calls to brightcove_field_refresh_video_entity_link()
brightcove_field_insert in ./brightcove.module
Implements hook_field_insert().
brightcove_field_update in ./brightcove.module
Implements hook_field_update().

File

./brightcove.module, line 1760
Brightcove module is an integration layer between any modules using Brightcove API. It makes all necessary checks for the API and makes settings available to the user.

Code

function brightcove_field_refresh_video_entity_link($entity_type, $entity, $items) {
  if ($entity_link_field = variable_get('brightcove_link_field')) {
    if ($entity_link = entity_uri($entity_type, $entity)) {
      $absolute_entity_link = url($entity_link['path'], [
        'absolute' => TRUE,
      ] + $entity_link['options']);
      $video_ids = [];

      // Get all videos grouped by the client.
      foreach ($items as $item) {
        $video_ids[$item['bcid']][] = $item['brightcove_id'];
      }
      if ($video_ids) {
        $videos = [];
        foreach ($video_ids as $bcid => $video_id) {
          $client = brightcove_client_load($bcid);
          brightcove_try(function () use (&$videos, $client, $video_id) {

            /** @var \Brightcove\API\CMS $cms */
            list($cms, ) = brightcove_create_classes($client);
            $videos = $cms
              ->listVideos('reference_id:' . implode(' ', $video_id));
          });
        }
        foreach ($videos as $video) {
          $custom_fields = $video
            ->getCustomFields();
          $custom_fields[$entity_link_field] = $absolute_entity_link;
          $video
            ->setCustomFields($custom_fields);
        }
      }
    }
  }
}