You are here

public static function FileDownloadLinkMedia::isApplicable in File Download Link 8

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

5 calls to FileDownloadLinkMedia::isApplicable()
FileDownloadLinkMediaApplicableTest::testApplicableAll in tests/src/Kernel/FileDownloadLinkMediaApplicableTest.php
This should not work for all until I delete oembed media type.
FileDownloadLinkMediaApplicableTest::testApplicableBoth in tests/src/Kernel/FileDownloadLinkMediaApplicableTest.php
This should be applicable for file media and image media.
FileDownloadLinkMediaApplicableTest::testApplicableFile in tests/src/Kernel/FileDownloadLinkMediaApplicableTest.php
This should be applicable for file media.
FileDownloadLinkMediaApplicableTest::testApplicableImage in tests/src/Kernel/FileDownloadLinkMediaApplicableTest.php
This should be applicable for image media.
FileDownloadLinkMediaApplicableTest::testApplicableOembed in tests/src/Kernel/FileDownloadLinkMediaApplicableTest.php
This should not work for oembed.

File

modules/file_downoad_link_media/src/Plugin/Field/FieldFormatter/FileDownloadLinkMedia.php, line 261

Class

FileDownloadLinkMedia
Plugin implementation of the 'file_download_link_media' formatter.

Namespace

Drupal\file_download_link_media\Plugin\Field\FieldFormatter

Code

public static function isApplicable(FieldDefinitionInterface $field_definition) {

  // This formatter is only available for entity types that reference
  // media items whose source field types are file and/or image.
  $target_type = $field_definition
    ->getFieldStorageDefinition()
    ->getSetting('target_type');
  if ($target_type != 'media') {
    return FALSE;
  }
  $media_bundles = isset($field_definition
    ->getSettings()['handler_settings']['target_bundles']) ? $field_definition
    ->getSettings()['handler_settings']['target_bundles'] : NULL;
  $media_types = MediaType::loadMultiple($media_bundles);
  foreach ($media_types as $media_type) {
    $source = $media_type
      ->getSource();
    $allowed_field_types = $source
      ->getPluginDefinition()['allowed_field_types'];
    if (!empty(array_diff($allowed_field_types, [
      'file',
      'image',
    ]))) {

      // In here means something other than file or image is allowed.
      return FALSE;
    }
  }
  return TRUE;
}