0

I want to embed the video as follows. I have written a FieldFormatter for this.

public function viewElements(FieldItemListInterface $items, $langcode) {
    $element = [];

    foreach ($items as $delta => $item) {
      // Render each element as markup.
        $target_id = $item->getValue()['target_id'];

        $file = \Drupal\file\Entity\File::load($target_id); //load the file and its attribute.        
        $parts = pathinfo($file->getFileUri());
        $new_filename = $parts['filename'] . '_' . $file->getCreatedTime() .'_selfhelpvideofid_' . $target_id . '.' . $parts['extension'];            

        $video = '<video width="480" height="320" controls="controls">
                    <source src="movie.mp4"  type="video/mp4">
                  </video>';    

      $element[$delta] = [
        '#type' => 'markup',
        '#markup' => $video,
      ];
    }

    return $element;
  }

The above is not working. Please help.

1

Instead of #markup, which is filtered, use inline template:

$file_path = 'movie.mp4'

$video = '<video width="480" height="320" controls="controls">
  <source src="{{ file_path }}"  type="video/mp4">
</video>';

$element[$delta] = array(
  '#type' => 'inline_template',
  '#template' => $video,
  '#context' => array(
    'file_path' => $file_path,
  ),
);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.