You are here

protected function BynderFormatter::getAttributesFieldsCandidates in Bynder 8

Same name and namespace in other branches
  1. 8.2 src/Plugin/Field/FieldFormatter/BynderFormatter.php \Drupal\bynder\Plugin\Field\FieldFormatter\BynderFormatter::getAttributesFieldsCandidates()

Gets list of fields that are candidates for IMG attributes.

Return value

array List of fields with machine names as keys and human-readable names as values.

2 calls to BynderFormatter::getAttributesFieldsCandidates()
BynderFormatter::settingsForm in src/Plugin/Field/FieldFormatter/BynderFormatter.php
Returns a form to configure settings for the formatter.
BynderFormatter::settingsSummary in src/Plugin/Field/FieldFormatter/BynderFormatter.php
Returns a short summary for the current formatter settings.

File

src/Plugin/Field/FieldFormatter/BynderFormatter.php, line 194

Class

BynderFormatter
Plugin implementation of the 'Bynder' formatter.

Namespace

Drupal\bynder\Plugin\Field\FieldFormatter

Code

protected function getAttributesFieldsCandidates() {
  $allowed_field_types = [
    'string',
    'string_long',
    'text',
    'text_long',
  ];
  $handler_settings = NULL;
  if (strpos($this->fieldDefinition
    ->getSetting('handler'), 'default:') === 0) {
    $handler_settings = $this->fieldDefinition
      ->getSetting('handler_settings');
  }
  $bundles = [];
  if ($handler_settings && is_array($handler_settings['target_bundles'])) {
    foreach ($handler_settings['target_bundles'] as $bundle) {

      /** @var \Drupal\media_entity\MediaBundleInterface $bundle_entity */
      $bundle_entity = $this->entityTypeManager
        ->getStorage('media_bundle')
        ->load($bundle);
      if ($bundle_entity && $bundle_entity
        ->getType() instanceof Bynder) {
        $bundles[] = $bundle_entity;
      }
    }
  }
  else {

    /** @var \Drupal\media_entity\MediaBundleInterface $bundle_entity */
    foreach ($this->entityTypeManager
      ->getStorage('media_bundle')
      ->loadMultiple() as $bundle_entity) {
      if ($bundle_entity && $bundle_entity
        ->getType() instanceof Bynder) {
        $bundles[] = $bundle_entity;
      }
    }
  }
  $options = [];
  foreach ($bundles as $bundle_entity) {
    foreach ($this->entityFieldManager
      ->getFieldDefinitions('media', $bundle_entity
      ->id()) as $field_name => $field) {
      if (in_array($field
        ->getType(), $allowed_field_types)) {
        $options[$field_name] = $field
          ->getLabel();
      }
    }
  }
  return $options;
}