You are here

function feeds_para_mapper_build_path in Feeds Paragraphs 7

Creates information array about each parent host field of the target field.

Parameters

array $field: The target field.

array $first_host: The first host field.

Return value

array List of the host fields.

1 call to feeds_para_mapper_build_path()
feeds_para_mapper_get_target_fields in ./feeds_para_mapper.module
Searches for any fields that a Paragraphs field has.

File

./feeds_para_mapper.module, line 218
Allows feeds to import content to Paragraphs' fields.

Code

function feeds_para_mapper_build_path(array $field, array $first_host) {
  $bundles = field_info_bundles('paragraphs_item');

  // Get bundles fields:
  foreach ($bundles as $name => $bundle) {
    $bundles[$name]['name'] = $name;
    $fields = field_info_instances('paragraphs_item', $name);
    $bundles[$name]['fields'] = $fields;
  }
  $field_bundle = NULL;
  $getFieldBundle = function ($field) use ($bundles) {
    foreach ($bundles as $bundle) {
      foreach ($bundle['fields'] as $b_field) {
        if ($b_field['field_name'] === $field['field_name']) {
          return $bundle;
        }
      }
    }
    return NULL;
  };
  $getHost = function ($field_bundle) use ($bundles) {
    foreach ($bundles as $bundle) {
      foreach ($bundle['fields'] as $b_field) {
        if (isset($b_field['settings']['allowed_bundles'])) {
          foreach ($b_field['settings']['allowed_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']['field_name'],
        'host_entity' => 'paragraphs_item',
      );
      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;
}