You are here

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

Maps a doc attribute to an entity.

Parameters

$entity object: A Drupal entity.

$doc object: A PMP doc.

$local_field string: The name of the field to which the attribute is being mapped.

$pmp_field string: The name of the doc attribute that is being mapped.

$pmp_type string: The type of the doc attribute (e.g., array)

1 call to PMPAPIDrupalPull::mapAttribute()
PMPAPIDrupalPull::mapFields in pmpapi_pull/classes/PMPAPIDrupalPull.php
Maps PMP doc attributes to entity fields.

File

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

Class

PMPAPIDrupalPull
Turns PMP hypermedia docs in Drupal entities.

Code

function mapAttribute($entity, $doc, $local_field, $pmp_field, $pmp_type) {
  $default_format = filter_default_format(user_load($this->uid));
  $local_field_info = field_info_field($local_field);
  if ($pmp_type == 'array') {
    if ($local_field_info['type'] == 'taxonomy_term_reference') {
      $tids = array();
      $vocab_name = $local_field_info['settings']['allowed_values'][0]['vocabulary'];
      $vocab = taxonomy_vocabulary_machine_name_load($vocab_name);
      $vid = $vocab->vid;
      foreach ($doc->attributes->{$pmp_field} as $element) {
        $terms = taxonomy_get_term_by_name($element, $vocab_name);
        if (!empty($terms)) {
          $term = array_shift($terms);
          $tids[] = array(
            'tid' => $term->tid,
          );
        }
        else {
          $term = new stdClass();
          $term->name = $element;
          $term->vid = $vid;
          $term->vocabulary_machine_name = $vocab->machine_name;
          taxonomy_term_save($term);
          $tids[] = array(
            'tid' => $term->tid,
          );
        }
      }
      $value = array(
        $entity->language => $tids,
      );
    }
    else {
      $value = implode(', ', $doc->{$pmp_field});
    }
  }
  else {
    $values = array(
      'value' => $doc->attributes->{$pmp_field},
      'format' => $default_format,
    );
    $this
      ->alterMappedValues($values, $local_field_info, $entity, $doc->profile);
    $value = array(
      $entity->language => array(
        0 => $values,
      ),
    );
  }
  $entity->{$local_field} = $value;
}