You are here

public function Twitter::attachConstraints in Media entity Twitter 8

Attaches type-specific constraints to media.

Parameters

MediaInterface $media: Media entity.

Overrides MediaTypeBase::attachConstraints

File

src/Plugin/MediaEntity/Type/Twitter.php, line 324

Class

Twitter
Provides media type plugin for Twitter.

Namespace

Drupal\media_entity_twitter\Plugin\MediaEntity\Type

Code

public function attachConstraints(MediaInterface $media) {
  parent::attachConstraints($media);
  if (isset($this->configuration['source_field'])) {
    $source_field_name = $this->configuration['source_field'];
    if ($media
      ->hasField($source_field_name)) {
      foreach ($media
        ->get($source_field_name) as &$embed_code) {

        /** @var \Drupal\Core\TypedData\DataDefinitionInterface $typed_data */
        $typed_data = $embed_code
          ->getDataDefinition();
        $typed_data
          ->addConstraint('TweetEmbedCode');
        $typed_data
          ->addConstraint('TweetVisible');
      }
    }
  }
}