You are here

private function NodeTypePreviewImageForm::collectImageFields in Acquia Content Hub 8

Collect image fields.

Traverse the FieldableEntity and its fields, collect a field "roadmap" that can lead to an 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 NodeTypePreviewImageForm::collectImageFields()
NodeTypePreviewImageForm::getForm in src/Form/NodeTypePreviewImageForm.php
Get Form.

File

src/Form/NodeTypePreviewImageForm.php, line 166

Class

NodeTypePreviewImageForm
Defines a form that alters node type form to add a preview image form.

Namespace

Drupal\acquia_contenthub\Form

Code

private function collectImageFields($target_type, $type, $key_prefix = '', $label_prefix = '') {
  $field_definitions = $this->entityFieldManager
    ->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->entityTypeManager
      ->getDefinition($field_target_type)
      ->entityClassImplements('\\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;
    }
  }
}