You are here

public static function BynderFormatterBase::isApplicable in Bynder 4.0.x

Same name and namespace in other branches
  1. 8.3 src/Plugin/Field/FieldFormatter/BynderFormatterBase.php \Drupal\bynder\Plugin\Field\FieldFormatter\BynderFormatterBase::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/BynderFormatterBase.php, line 175

Class

BynderFormatterBase
Base class for Bynder formatters.

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\MediaTypeInterface $type */
            $type = \Drupal::entityTypeManager()
              ->getStorage('media_type')
              ->load($bundle);
            if ($type
              ->getSource() 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\MediaTypeInterface $type_entity */
    if (!($type_entity = $field_definition
      ->getTargetBundle()) || !($type_entity = MediaType::load($field_definition
      ->getTargetBundle()))) {
      return FALSE;
    }
    $source = $type_entity
      ->getSource();
    if (!$source instanceof Bynder) {
      return FALSE;
    }
    if ($source
      ->getConfiguration()['source_field'] != $field_definition
      ->getName()) {
      return FALSE;
    }
    return TRUE;
  }
}