You are here

public function ContentHubEntityDependency::getRemoteDependencies in Acquia Content Hub 8

Obtains remote dependencies for this particular entity.

Return value

array An array of UUIDs

File

src/ContentHubEntityDependency.php, line 231

Class

ContentHubEntityDependency
Content Hub Dependency Class.

Namespace

Drupal\acquia_contenthub

Code

public function getRemoteDependencies() {
  $dependencies = [];

  // Finding assets (files) dependencies.
  foreach ($this->cdf
    ->getAssets() as $asset) {
    preg_match('#\\[(.*)\\]#', $asset['replace-token'], $match);
    $uuid = $match[1];
    if (Uuid::isValid($uuid)) {

      // It is a valid UUID => Then it should refer to an entity.
      $dependencies[] = $uuid;
    }
  }

  // Adding this exclude some attributes, which we don't want to take into
  // consideration the dependency information contained on them.
  $excluded_attributes = $this
    ->getExcludedAttributesFromDependencies();

  // Check if 'entity_embed' exists.
  $exists_entity_embed = \Drupal::moduleHandler()
    ->moduleExists('entity_embed');

  // Finding attributes (entity references) dependencies.
  foreach ($this->cdf
    ->getAttributes() as $name => $attribute) {
    if (!in_array($name, $excluded_attributes)) {
      $type = $attribute['type'];
      if ($type == Attribute::TYPE_REFERENCE) {

        // Obtaining values for every language.
        $languages = array_keys($attribute['value']);
        foreach ($languages as $lang) {
          $dependencies[] = $attribute['value'][$lang];
        }
      }
      elseif ($type == Attribute::TYPE_ARRAY_REFERENCE) {

        // Obtaining values for every language.
        $languages = array_keys($attribute['value']);
        foreach ($languages as $lang) {
          $dependencies = array_merge($dependencies, $attribute['value'][$lang]);
        }
      }
      elseif ($type == Attribute::TYPE_ARRAY_STRING) {

        // Obtaining values for every language.
        $languages = array_keys($attribute['value']);
        foreach ($languages as $lang) {
          if (!is_array($attribute['value'][$lang])) {
            continue;
          }

          // Process all text items.
          foreach ($attribute['value'][$lang] as $item) {
            $field = json_decode($item, TRUE);
            $value = isset($field['value']) ? $field['value'] : '';
            if ($exists_entity_embed && !empty($value)) {

              // Parse uuid from text.
              $entity_embed_handler = new ContentHubEntityEmbedHandler();
              $uuids = $entity_embed_handler
                ->getReferencedUuids($value);
              $dependencies = array_merge($dependencies, $uuids);
            }

            // Check for the existence of Link Fields that might link to
            // other entities (which should be considered dependencies).
            $entity_info = ContentHubEntityLinkFieldHandler::load()
              ->getDependentEntityInfo($field);
            if (!empty($entity_info)) {
              $uuid = $entity_info[1];
              $dependencies[] = $uuid;
            }
          }
        }
      }
    }
  }
  return array_unique($dependencies);
}