You are here

function _datalayer_get_entity_data in dataLayer 7

Same name and namespace in other branches
  1. 8 datalayer.module \_datalayer_get_entity_data()

Collect entity data for output and altering.

Parameters

object $obj: Entity object of the page menu callback.

string $type: String representing entitie's type.

Return value

array All properties and values for output of page entity.

2 calls to _datalayer_get_entity_data()
DataLayerUnitTests::testDataLayerGetEntityDataReturnsEntityDataArray in tests/datalayer.unit.test
Test DataLayer Get Entity Terms Returns Entity Data Array.
datalayer_get_page_data in ./datalayer.module
Return all the page meta data.

File

./datalayer.module, line 260
Client-side data space.

Code

function _datalayer_get_entity_data($obj, $type) {
  $output_data =& drupal_static(__FUNCTION__);
  if (empty($output_data)) {

    // Explicit additions and generalized properties...
    $entity_info = entity_get_info($type);
    $bundle = FALSE;

    // Entity type.
    $output_data['entityType'] = $type;

    // Entity bundle.
    if (isset($obj->{$entity_info['entity keys']['bundle']})) {
      $bundle = $obj->{$entity_info['entity keys']['bundle']};
      $output_data['entityBundle'] = $bundle;
    }

    // Entity indetifier.
    if (isset($obj->{$entity_info['entity keys']['id']})) {
      $output_data['entityId'] = $obj->{$entity_info['entity keys']['id']};
    }

    // Entity title.
    if (isset($entity_info['entity keys']) && isset($entity_info['entity keys']['label']) && isset($obj->{$entity_info['entity keys']['label']})) {
      $output_data['entityLabel'] = $obj->{$entity_info['entity keys']['label']};
    }
    elseif ($entity_info['label'] === 'User') {

      // User entities don't report a label.
      $output_data['entityLabel'] = $obj->name;
    }

    // Output various entity properties. Allow additions/alterations.
    // NOTE: Properties mean different things on different entity types.
    $properties = _datalayer_collect_meta_properties($type);
    $entity_meta = array_filter(variable_get('datalayer_global_entity_meta', array()));
    $selected_properties = _datalayer_get_selected_properties($properties, $entity_meta);
    $output_data = array_merge(_datalayer_collect_meta_values($selected_properties, $obj), $output_data);

    // Output term data.
    if (variable_get('datalayer_output_terms', module_exists('taxonomy'))) {
      $selected_vocabs = array_filter(variable_get('datalayer_vocabs', array()));
      if ($type == 'taxonomy_term') {
        $output_data['entityTaxonomy'] = array(
          $obj->vocabulary_machine_name => array(
            $obj->tid => $obj->name,
          ),
        );
      }
      else {

        // Meta data on content.
        if ($taxonomy = _datalayer_get_entity_terms($type, $bundle, $obj)) {

          // Limit configured vocabs.
          if (empty($selected_vocabs)) {
            $output_data['entityTaxonomy'] = $taxonomy;
          }
          else {
            foreach ($taxonomy as $vocab => $terms) {
              if (isset($selected_vocabs[$vocab])) {
                $output_data['entityTaxonomy'][$vocab] = $terms;
              }
            }
          }
        }
      }
    }
  }
  return $output_data;
}