You are here

public function AssetMetadataHelper::getMetadataFromAsset in Media: Acquia DAM 8

Gets a metadata item from the given asset.

Parameters

\cweagans\webdam\Entity\Asset $asset: The asset to get metadata from.

string $name: The name of the metadata item to retrieve.

Return value

mixed Result will vary based on the metadata item.

File

src/Service/AssetMetadataHelper.php, line 163

Class

AssetMetadataHelper
Class AssetMetadataHelper.

Namespace

Drupal\media_acquiadam\Service

Code

public function getMetadataFromAsset(Asset $asset, $name) {

  // Return values of XMP metadata.
  $xmpMetadataFields = $this
    ->getMetadataXmpFields();
  if (array_key_exists($name, $xmpMetadataFields)) {

    // Strip 'xmp_' prefix to retrieve matching asset xmp metadata.
    $xmp_field = substr($name, 4);
    return isset($asset->xmp_metadata[$xmp_field]['value']) ? $asset->xmp_metadata[$xmp_field]['value'] : NULL;
  }
  switch ($name) {
    case 'folderID':
      return isset($asset->folder->id) ? $asset->folder->id : NULL;
    case 'status':
      return isset($asset->status) ? intval($asset->status == 'active') : NULL;
    case 'type':
      if (isset($asset->type_id)) {
        $type_mapping = [
          1 => 'Image',
          2 => 'Video',
          3 => 'Document',
          4 => 'Presentation',
          5 => 'Other',
        ];
        return $type_mapping[$asset->type_id] ?: NULL;
      }
      return NULL;
    case 'datecaptured_date':
    case 'datecreated_date':
    case 'datemodified_date':
      $date_property_mapping = [
        'datecaptured_date' => 'datecapturedUnix',
        'datecreated_date' => 'date_created_unix',
        'datemodified_date' => 'date_modified_unix',
      ];
      $date_property = $date_property_mapping[$name];
      if (!empty($asset->{$date_property})) {

        // html_datetime includes the timezone so we must use a custom format.
        return $this->dateFormatter
          ->format($asset->{$date_property}, 'custom', 'Y-m-d\\TH:i:s');
      }
      return NULL;
    default:

      // The key should be the local property name and the value should be the
      // DAM provided property name.
      $property_name_mapping = [
        'colorspace' => 'colorspace',
        'datecaptured' => 'datecaptured',
        'datecreated' => 'datecreated',
        'datemodified' => 'datemodified',
        'datecaptured_unix' => 'datecapturedUnix',
        'datecreated_unix' => 'date_created_unix',
        'datemodified_unix' => 'date_modified_unix',
        'description' => 'description',
        'filename' => 'filename',
        'filesize' => 'filesize',
        'filetype' => 'filetype',
        'height' => 'height',
        'id' => 'id',
        'type_id' => 'type_id',
        'version' => 'version',
        'width' => 'width',
      ];
      if (array_key_exists($name, $property_name_mapping)) {
        $property_name = $property_name_mapping[$name];
        return isset($asset->{$property_name}) ? $asset->{$property_name} : NULL;
      }
  }
  return NULL;
}