You are here

public static function AmpMediaImageFormatter::isApplicable in Accelerated Mobile Pages (AMP) 8.3

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/AmpMediaImageFormatter.php, line 57

Class

AmpMediaImageFormatter
Plugin for amp media image formatter.

Namespace

Drupal\amp\Plugin\Field\FieldFormatter

Code

public static function isApplicable(FieldDefinitionInterface $field_definition) {
  $settings = $field_definition
    ->getSettings();

  // This is an entity_reference that does not target media, this formatter
  // does not apply.
  if ($settings['target_type'] != 'media') {
    return FALSE;
  }

  // This field targets all media bundles, which might include images, this
  // formatter applies.
  if (empty($settings['handler_settings']) || empty($settings['handler_settings']['target_bundles'])) {
    return TRUE;
  }
  else {
    foreach ($settings['handler_settings']['target_bundles'] as $bundle) {
      $media_type = MediaType::load($bundle);

      // This field specifically targets bundles of the Image media type, this
      // formatter applies
      if ($media_type && $media_type
        ->getSource() instanceof Image) {
        return TRUE;
      }
    }
  }

  // None of the above is true, this field targets non-image media, this
  // formatter does not apply.
  return FALSE;
}