getSourceFieldValue example

/** * Tests the getSourceFieldValue() method. */
  public function testGetSourceFieldValue() {
    /** @var \Drupal\media\MediaInterface $media */
    $media = Media::create([
      'bundle' => $this->testMediaType->id(),
      'field_media_test' => 'some_value',
    ]);
    $media->save();
    $media_source = $media->getSource();
    $this->assertSame('some_value', $media_source->getSourceFieldValue($media));

    // Test that NULL is returned if there is no value in the source field.     $media->set('field_media_test', NULL)->save();
    $this->assertNull($media_source->getSourceFieldValue($media));
  }

  /** * Tests the thumbnail functionality. */
  public function testThumbnail() {
    file_put_contents('public://thumbnail1.jpg', '');
    
'url' => $this->t('Resource source URL'),
      'width' => $this->t('Resource width'),
      'height' => $this->t('Resource height'),
      'html' => $this->t('Resource HTML representation'),
    ];
  }

  /** * {@inheritdoc} */
  public function getMetadata(MediaInterface $media$name) {
    $media_url = $this->getSourceFieldValue($media);
    // The URL may be NULL if the source field is empty, in which case just     // return NULL.     if (empty($media_url)) {
      return NULL;
    }

    try {
      $resource_url = $this->urlResolver->getResourceUrl($media_url);
      $resource = $this->resourceFetcher->fetchResource($resource_url);
    }
    catch (ResourceException $e) {
      

  protected function hasSourceFieldChanged() {
    $source = $this->getSource();
    return isset($this->original) && $source->getSourceFieldValue($this) !== $source->getSourceFieldValue($this->original);
  }

  /** * Determines if the thumbnail should be updated for a media item. * * @param bool $is_new * Specifies whether the media item is new. * * @return bool * TRUE if the thumbnail should be updated, FALSE otherwise. */
  

  public function validate($value, Constraint $constraint) {
    /** @var \Drupal\media\MediaInterface $media */
    $media = $value->getEntity();
    /** @var \Drupal\media\Plugin\media\Source\OEmbedInterface $source */
    $source = $media->getSource();

    if (!($source instanceof OEmbedInterface)) {
      throw new \LogicException('Media source must implement ' . OEmbedInterface::class);
    }
    $url = $source->getSourceFieldValue($media);
    // The URL may be NULL if the source field is empty, which is invalid input.     if (empty($url)) {
      $this->context->addViolation($constraint->invalidResourceMessage);
      return;
    }

    // Ensure that the URL matches a provider.     try {
      $provider = $this->urlResolver->getProviderByUrl($url);
    }
    catch (ResourceException $e) {
      
Home | Imprint | This part of the site doesn't use cookies.