You are here

protected function BynderFormatter::getAttributesFieldsCandidates in Bynder 8.2

Same name and namespace in other branches
  1. 8 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 203

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\MediaTypeInterface $type */
      $type = $this->entityTypeManager
        ->getStorage('media_type')
        ->load($bundle);
      if ($type && $type
        ->getSource() instanceof Bynder) {
        $bundles[] = $type;
      }
    }
  }
  else {

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