You are here

function _datalayer_get_entity_data in dataLayer 8

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

Collect entity data for output and altering.

Parameters

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

Return value

array All properties and values for output of page entity.

2 calls to _datalayer_get_entity_data()
DataLayerKernelTest::testDataLayerGetEntityDataReturnsEntityDataArray in tests/src/Kernel/DataLayerKernelTest.php
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 251
Client-side data space.

Code

function _datalayer_get_entity_data($entity) {
  $output_data =& drupal_static(__FUNCTION__);
  if (empty($output_data)) {
    $datalayer_settings = \Drupal::config('datalayer.settings');

    // Explicit additions and generalized properties...
    $type = $entity
      ->getEntityTypeId();
    $entity_info = \Drupal::entityTypeManager()
      ->getDefinition($type);
    $entity_keys = $entity_info
      ->getKeys();
    $bundle = FALSE;

    // Entity type.
    $output_data[$datalayer_settings
      ->get('entity_type')] = $type;

    // Entity bundle.
    if (isset($entity->{$entity_keys['bundle']})) {
      $bundle = $entity->{$entity_keys['bundle']}
        ->getString();
      $output_data[$datalayer_settings
        ->get('entity_bundle')] = $bundle;
    }

    // Entity indetifier.
    if (isset($entity->{$entity_keys['id']})) {
      $id = $entity->{$entity_keys['id']}
        ->getString();
      $output_data[$datalayer_settings
        ->get('entity_identifier')] = $id;
    }

    // Entity title.
    if (isset($entity_keys['label']) && isset($entity->{$entity_keys['label']})) {
      $label = $entity->{$entity_keys['label']}
        ->getString();
      $output_data[$datalayer_settings
        ->get('entity_title')] = $label;
    }
    elseif ($entity_info
      ->id() === 'user') {

      // User entities don't report a label entity key.
      $output_data[$datalayer_settings
        ->get('entity_title')] = $entity
        ->label();
    }

    // Output various entity properties. Allow additions/alterations.
    // NOTE: Properties mean different things on different entity types.
    $properties = _datalayer_collect_meta_properties($type);
    $entity_meta = $datalayer_settings
      ->get('entity_meta');
    $selected_properties = _datalayer_get_selected_properties($properties, $entity_meta);
    $output_data = array_merge(_datalayer_collect_meta_values($selected_properties, $entity), $output_data);

    // Output group name.
    if ($datalayer_settings
      ->get('group')) {
      if (\Drupal::moduleHandler()
        ->moduleExists('group')) {
        if ($entity instanceof EntityInterface && $entity
          ->getEntityType() == 'node') {
          $groupName = datalayer_get_entity_group($entity);
          if (!is_null($groupName)) {
            $output_data[$datalayer_settings
              ->get('group_label')] = $groupName
              ->label();
          }
        }
      }
    }

    // Output path based IA values.
    if ($datalayer_settings
      ->get('enable_ia')) {
      $depth = $datalayer_settings
        ->get('ia_depth');

      // Retrieve an array which contains the path pieces.
      $current_path = \Drupal::service('path.current')
        ->getPath();
      $result = \Drupal::service('path.alias_manager')
        ->getAliasByPath($current_path);
      $path_args = explode('/', $result);
      $i = 0;
      foreach ($path_args as $component) {
        if ($i != $depth) {
          if (!empty($component)) {
            if ($i == 0) {
              $category = $datalayer_settings
                ->get('ia_category_primary');
            }
            else {
              $category = $datalayer_settings
                ->get('ia_category_sub') . $i;
            }
            $output_data[$category] = $component;
            $i++;
          }
        }
      }
    }

    // Output field data.
    if ($datalayer_settings
      ->get('output_fields')) {
      $fields = _datalayer_get_field_values($entity);
      $replacements = $datalayer_settings
        ->get('key_replacements');
      foreach ($fields as $key => $value) {
        if (array_key_exists($key, $replacements)) {
          $key = $replacements[$key];
        }
        $output_data[$key] = $value;
      }
    }

    // Output term data.
    if ($datalayer_settings
      ->get('output_terms')) {
      $vocabs = $datalayer_settings
        ->get('vocabs');
      $selected_vocabs = $vocabs ? array_filter($vocabs) : NULL;
      if ($type == 'taxonomy_term') {
        $output_data['entityTaxonomy'] = [
          $entity->vid
            ->getString() => [
            $entity->tid
              ->getString() => $entity
              ->label(),
          ],
        ];
      }
      else {

        // Meta data on content.
        if ($taxonomy = _datalayer_get_entity_terms($entity)) {

          // 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;
}