You are here

public static function MapperHelper::getSubFields in Feeds Paragraphs 8

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

File

src/Utility/MapperHelper.php, line 57

Class

MapperHelper

Namespace

Drupal\feeds_para_mapper\Utility

Code

public static function getSubFields(FieldConfigInterface $target, $with_path = FALSE, array $result = array(), array $first_host = array()) {
  $settings = $target
    ->getSettings();
  $target_bundles = $settings['handler_settings']['target_bundles'];
  $target_bundles = array_values($target_bundles);
  foreach ($target_bundles as $target_bundle) {
    $entityManager = \Drupal::service('entity_field.manager');
    $sub_fields = $entityManager
      ->getFieldDefinitions('paragraph', $target_bundle);
    $sub_fields = array_filter($sub_fields, function ($item) {
      return $item instanceof FieldConfigInterface;
    });
    foreach ($sub_fields as $machine_name => $sub_field) {

      // Initialize first host:
      if ($target
        ->getTargetEntityTypeId() !== 'paragraph') {
        $first_host = array(
          'bundle' => $target_bundle,
          'host_field' => $target
            ->getName(),
          'host_entity' => $target
            ->getTargetEntityTypeId(),
        );
      }

      // If we found nested Paragraphs field,
      // loop through its sub fields to include them:
      if ($sub_field
        ->getType() === 'entity_reference_revisions') {
        $result = self::getSubFields($sub_field, $with_path, $result, $first_host);
      }
      else {
        if ($sub_field
          ->getType() !== "feeds_item") {
          $host_allowed = $target
            ->getFieldStorageDefinition()
            ->getCardinality();
          $fieldAllowed = $sub_field
            ->getFieldStorageDefinition()
            ->getCardinality();
          $unlimited = FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED;
          $has_settings = FALSE;
          if ($host_allowed === $unlimited || $host_allowed > 1) {
            if ($fieldAllowed === $unlimited || $fieldAllowed > 1) {
              $has_settings = TRUE;
            }
          }
          self::updateInfo($sub_field, "has_settings", $has_settings);

          // @todo: continue here
          self::setFieldsInCommon($sub_field, $result);
          if ($with_path) {
            $path = self::buildPath($sub_field, $first_host);
            self::updateInfo($sub_field, "path", $path);
          }
          $result[] = $sub_field;
        }
      }
    }
  }
  return $result;
}