You are here

function PMPAPIDrupalPull::addItems in Public Media Platform API Integration 7

Maps PMP doc items to entity fields.

Parameters

object $entity: Any drupal entity object

object $doc: A PMP doc

array $map: A mapping of PMP profile attributes (etc.) to entity fields

1 call to PMPAPIDrupalPull::addItems()
PMPAPIDrupalPull::saveEntity in pmpapi_pull/classes/PMPAPIDrupalPull.php
Saves a PMP doc as an entity.

File

pmpapi_pull/classes/PMPAPIDrupalPull.php, line 443
Contains PMPAPIDrupalPull.

Class

PMPAPIDrupalPull
Turns PMP hypermedia docs in Drupal entities.

Code

function addItems($entity, $doc, $map) {
  if (!empty($doc->items) && (empty($doc->recurse) || !isset($doc->recurse))) {
    foreach ($doc->items as $item) {
      $pieces = explode('/', $item->links->profile[0]->href);
      $item_profile = end($pieces);
      $local_field = !empty($map['item-' . $item_profile]) ? $map['item-' . $item_profile] : NULL;
      if ($local_field && ($item_profile == 'image' || $item_profile == 'audio' || $item_profile == 'video')) {
        $item_guid = $item->attributes->guid;
        $item_pmp = pmpapi_fetch($item_guid);
        if (is_object($item_pmp) && !empty($item_pmp->query->results->docs) && empty($item_pmp->errors)) {
          $item_doc = $item_pmp->query->results->docs[0];
          $item_doc->recurse = FALSE;
          $item_doc->pmpapi_pull = TRUE;
          $this
            ->saveEntity($item_doc);
          $eid = pmpapi_get_eid_from_guid($item_guid);
          $local_field_info = field_info_field($local_field);
          $mapping = array(
            'image' => array(
              'fid' => $eid,
              'display' => 1,
            ),
            'file' => array(
              'fid' => $eid,
              'display' => 1,
            ),
            'entityreference' => array(
              'target_id' => $eid,
            ),
          );

          // Attach entity reference
          if (!$this
            ->reference_exists($entity, $local_field_info['type'], $local_field, $eid)) {
            $entity->{$local_field}[$entity->language][] = $mapping[$local_field_info['type']];
          }
        }
        else {
          drupal_set_message(t('No related doc could be found in the PMP with this guid.'), 'warning');
        }
      }
    }
  }
}