You are here

private function Mapper::buildPath in Feeds Paragraphs 8

Gets the field path (in case its nested)

Parameters

FieldDefinitionInterface $field:

array $first_host:

Return value

array

2 calls to Mapper::buildPath()
Mapper::getSubFields in src/Mapper.php
Mapper::isWrapped in src/Mapper.php

File

src/Mapper.php, line 295

Class

Mapper

Namespace

Drupal\feeds_para_mapper

Code

private function buildPath(FieldDefinitionInterface $field, array $first_host) {
  $bundles = $this->bundleInfo
    ->getBundleInfo('paragraph');

  // Get bundles fields:
  foreach ($bundles as $name => $bundle) {
    $bundles[$name]['name'] = $name;
    $fields = $this->entityFieldManager
      ->getFieldDefinitions('paragraph', $name);
    $fields = array_filter($fields, function ($item) {
      return $item instanceof FieldConfigInterface;
    });
    $bundles[$name]['fields'] = $fields;
  }
  $field_bundle = NULL;
  $getFieldBundle = function (FieldDefinitionInterface $field) use ($bundles) {
    foreach ($bundles as $bundle) {
      foreach ($bundle['fields'] as $b_field) {
        if ($b_field
          ->getName() === $field
          ->getName()) {
          return $bundle;
        }
      }
    }
    return NULL;
  };
  $getHost = function ($field_bundle) use ($bundles) {
    foreach ($bundles as $bundle) {
      foreach ($bundle['fields'] as $b_field) {
        $settings = $b_field
          ->getSetting('handler_settings');
        if (isset($settings['target_bundles'])) {
          foreach ($settings['target_bundles'] as $allowed_bundle) {
            if ($allowed_bundle === $field_bundle['name']) {

              /*
                              Get the allowed bundle and set it as the host bundle.
                              This grabs the first allowed bundle,
                              and might cause issues with multiple bundles field.
                              todo: Test with multiple bundles field.
              */
              $allowed = array_filter($bundles, function ($item) use ($allowed_bundle) {
                return $item['name'] === $allowed_bundle;
              });
              $allowed = array_values($allowed);
              return array(
                'bundle' => $allowed[0],
                'host_field' => $b_field,
              );
            }
          }
        }
      }
    }
    return NULL;
  };

  // Start building the path:
  $path = array();
  $field_bundle = $getFieldBundle($field);
  while (isset($field_bundle)) {
    $host = $getHost($field_bundle);
    if (isset($host)) {
      $new_path = array(
        'bundle' => $host['bundle']['name'],
        'host_field' => $host['host_field']
          ->getName(),
        'host_field_bundle' => $host['host_field']
          ->get('bundle'),
        'host_entity' => 'paragraph',
      );
      array_unshift($path, $new_path);
      $field_bundle = $getFieldBundle($host['host_field']);
    }
    else {
      $field_bundle = NULL;
    }
  }

  // Add the first host to the path:
  array_unshift($path, $first_host);

  // Add order to all path items:
  for ($i = 0; $i < count($path); $i++) {
    $path[$i]['order'] = $i;
  }
  return $path;
}