You are here

public static function BynderFormatter::isApplicable 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::isApplicable()

Returns if the formatter can be used for the provided field.

Parameters

\Drupal\Core\Field\FieldDefinitionInterface $field_definition: The field definition that should be checked.

Return value

bool TRUE if the formatter can be used, FALSE otherwise.

Overrides FormatterBase::isApplicable

File

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

Class

BynderFormatter
Plugin implementation of the 'Bynder' formatter.

Namespace

Drupal\bynder\Plugin\Field\FieldFormatter

Code

public static function isApplicable(FieldDefinitionInterface $field_definition) {
  if ($field_definition
    ->getType() == 'entity_reference') {
    if ($field_definition
      ->getFieldStorageDefinition()
      ->getSetting('target_type') == 'media') {
      if (strpos($field_definition
        ->getSetting('handler'), 'default:') === 0) {
        $handler_settings = $field_definition
          ->getSetting('handler_settings');
        if ($handler_settings['target_bundles'] === NULL) {
          return TRUE;
        }
        elseif (is_array($handler_settings['target_bundles'])) {
          foreach ($handler_settings['target_bundles'] as $bundle) {

            /** @var \Drupal\media_entity\MediaBundleInterface $bundle_entity */
            $bundle_entity = \Drupal::entityTypeManager()
              ->getStorage('media_bundle')
              ->load($bundle);
            if ($bundle_entity
              ->getType() instanceof Bynder) {
              return TRUE;
            }
          }
        }
      }
      else {

        // If some other selection plugin than default is used we can't
        // reliably determine if we apply or not so we allow.
        return TRUE;
      }
    }
    return FALSE;
  }
  elseif (in_array($field_definition
    ->getType(), [
    'string',
    'string_long',
  ])) {
    if ($field_definition
      ->getTargetEntityTypeId() != 'media') {
      return FALSE;
    }

    /** @var \Drupal\media_entity\MediaBundleInterface $bundle */
    if (!($bundle = $field_definition
      ->getTargetBundle()) || !($bundle = MediaBundle::load($field_definition
      ->getTargetBundle()))) {
      return FALSE;
    }
    $type = $bundle
      ->getType();
    if (!$type instanceof Bynder) {
      return FALSE;
    }
    if ($type
      ->getConfiguration()['source_field'] != $field_definition
      ->getName()) {
      return FALSE;
    }
    return TRUE;
  }
}