You are here

trait ValidationConstraintMatchTrait in Lightning Media 8

Same name and namespace in other branches
  1. 8.4 src/ValidationConstraintMatchTrait.php \Drupal\lightning_media\ValidationConstraintMatchTrait
  2. 8.2 src/ValidationConstraintMatchTrait.php \Drupal\lightning_media\ValidationConstraintMatchTrait
  3. 8.3 src/ValidationConstraintMatchTrait.php \Drupal\lightning_media\ValidationConstraintMatchTrait

Implements InputMatchInterface for media types that use an embed code or URL.

Hierarchy

2 files declare their use of ValidationConstraintMatchTrait
Instagram.php in modules/lightning_media_instagram/src/Plugin/media/Source/Instagram.php
Twitter.php in modules/lightning_media_twitter/src/Plugin/media/Source/Twitter.php

File

src/ValidationConstraintMatchTrait.php, line 11

Namespace

Drupal\lightning_media
View source
trait ValidationConstraintMatchTrait {

  /**
   * Returns the typed data manager.
   *
   * @return \Drupal\Core\TypedData\TypedDataManagerInterface
   *   The typed data manager.
   */
  private function typedDataManager() {
    return @($this->typedDataManager ?: \Drupal::typedDataManager());
  }

  /**
   * Implements InputMatchInterface::appliesTo().
   */
  public function appliesTo($value, MediaTypeInterface $media_type) {
    $plugin_definition = $this
      ->getPluginDefinition();
    $definition = $this
      ->typedDataManager()
      ->createDataDefinition('string')
      ->addConstraint($plugin_definition['input_match']['constraint']);
    $data = StringData::createInstance($definition);
    $data
      ->setValue($value);
    return $data
      ->validate()
      ->count() === 0;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ValidationConstraintMatchTrait::appliesTo public function Implements InputMatchInterface::appliesTo().
ValidationConstraintMatchTrait::typedDataManager private function Returns the typed data manager.