You are here

private function NodeTypeThumbnailFormHelper::collectImageFields in Acquia Lift Connector 8

Traverse the FieldableEntity and its fields, collect a field "roadmap" that can lead to a image file.

Parameters

string $target_type: Fieldable entity's identifier.

string $type: Type of the fieldable entity.

string $key_prefix: The concatenated entity field keys that has been traversed through.

string $label_prefix: The concatenated entity labels that has been traversed through.

1 call to NodeTypeThumbnailFormHelper::collectImageFields()
NodeTypeThumbnailFormHelper::getForm in src/Service/Helper/NodeTypeThumbnailFormHelper.php
Get Form.

File

src/Service/Helper/NodeTypeThumbnailFormHelper.php, line 145
Contains \Drupal\acquia_lift\Service\Helper\NodeTypeThumbnailFormHelper.

Class

NodeTypeThumbnailFormHelper
Defines a form that alters node type form to add a thumbnail form.

Namespace

Drupal\acquia_lift\Service\Helper

Code

private function collectImageFields($target_type, $type, $key_prefix = '', $label_prefix = '') {
  $field_definitions = $this->entityManager
    ->getFieldDefinitions($target_type, $type);
  foreach ($field_definitions as $field_key => $field_definition) {
    $field_type = $field_definition
      ->getType();
    $field_target_type = $field_definition
      ->getSetting('target_type');
    $field_label = $field_definition
      ->getLabel();
    $full_label = $label_prefix . $field_label;
    $full_key = $key_prefix . $field_key;

    // 1) Image type.
    if ($field_type === 'image') {
      $this->imageFields[$full_key] = $full_label . ' (' . $full_key . ')';
      continue;
    }

    // Check if the field has already been processed. If so, skip.
    $field_hash = spl_object_hash($field_definition);
    if (isset($this->processedFieldHashes[$field_hash])) {
      continue;
    }

    // 2) Entity Reference type whose entity is Fieldable.
    if ($field_type === 'entity_reference' && $this->entityManager
      ->getDefinition($field_target_type)
      ->isSubclassOf('\\Drupal\\Core\\Entity\\FieldableEntityInterface')) {

      // Track this field, since it is about to be processed.
      $this->processedFieldHashes[$field_hash] = TRUE;

      // Process this field.
      $this
        ->collectImageFields($field_target_type, $field_type, $full_key . '->', $full_label . '->');
      continue;
    }
  }
}