You are here

public static function MapperHelper::buildPath in Feeds Paragraphs 8

1 call to MapperHelper::buildPath()
MapperHelper::getSubFields in src/Utility/MapperHelper.php

File

src/Utility/MapperHelper.php, line 105

Class

MapperHelper

Namespace

Drupal\feeds_para_mapper\Utility

Code

public static function buildPath(FieldConfigInterface $field, array $first_host) {
  $bundles = \Drupal::service('entity_type.bundle.info')
    ->getBundleInfo('paragraph');

  // Get bundles fields:
  foreach ($bundles as $name => $bundle) {
    $bundles[$name]['name'] = $name;
    $fields = \Drupal::getContainer()
      ->get('entity_field.manager')
      ->getFieldDefinitions('paragraph', $name);
    $fields = array_filter($fields, function ($item) {
      return $item instanceof FieldConfigInterface;
    });
    $bundles[$name]['fields'] = $fields;
  }
  $field_bundle = NULL;
  $getFieldBundle = function (FieldConfigInterface $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_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;
}