You are here

public function BlazyAdminFormatterBase::getFieldOptions in Blazy 8

Same name and namespace in other branches
  1. 8.2 src/Form/BlazyAdminFormatterBase.php \Drupal\blazy\Form\BlazyAdminFormatterBase::getFieldOptions()
  2. 7 src/Form/BlazyAdminFormatterBase.php \Drupal\blazy\Form\BlazyAdminFormatterBase::getFieldOptions()

Returns available fields for select options.

File

src/Form/BlazyAdminFormatterBase.php, line 204

Class

BlazyAdminFormatterBase
A base for field formatter admin to have re-usable methods in one place.

Namespace

Drupal\blazy\Form

Code

public function getFieldOptions($target_bundles = [], $allowed_field_types = [], $entity_type = 'media', $target_type = '') {
  $options = [];
  $storage = $this
    ->blazyManager()
    ->getEntityTypeManager()
    ->getStorage('field_config');

  // Fix for Views UI not recognizing Media bundles, unlike Formatters.
  if (empty($target_bundles)) {
    $bundle_service = \Drupal::service('entity_type.bundle.info');
    $target_bundles = $bundle_service
      ->getBundleInfo($entity_type);
  }

  // Declutters options from less relevant options.
  $excludes = $this
    ->getExcludedFieldOptions();
  foreach ($target_bundles as $bundle => $label) {
    if ($fields = $storage
      ->loadByProperties([
      'entity_type' => $entity_type,
      'bundle' => $bundle,
    ])) {
      foreach ((array) $fields as $field) {
        if (in_array($field
          ->getName(), $excludes)) {
          continue;
        }
        if (empty($allowed_field_types)) {
          $options[$field
            ->getName()] = $field
            ->getLabel();
        }
        elseif (in_array($field
          ->getType(), $allowed_field_types)) {
          $options[$field
            ->getName()] = $field
            ->getLabel();
        }
        if (!empty($target_type) && $field
          ->getSetting('target_type') == $target_type) {
          $options[$field
            ->getName()] = $field
            ->getLabel();
        }
      }
    }
  }
  return $options;
}