Adds extracted text to Ableplayer Caption media
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

74 lines
2.2 KiB

<?php
declare(strict_types=1);
namespace Drupal\transcript_action\Plugin\Action;
use Drupal\Core\Access\AccessResultInterface;
use Drupal\Core\Action\ActionBase;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Session\AccountInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Provides an Extract Transcription text action.
*
* @Action(
* id = "transcript_action_extract_transcription_text",
* label = @Translation("Extract Transcription text"),
* type = "media",
* category = @Translation("Custom"),
* )
*
* @DCG
* For updating entity fields consider extending FieldUpdateActionBase.
* @see \Drupal\Core\Field\FieldUpdateActionBase
*
* @DCG
* In order to set up the action through admin interface the plugin has to be
* configurable.
* @see https://www.drupal.org/project/drupal/issues/2815301
* @see https://www.drupal.org/project/drupal/issues/2815297
*
* @DCG
* The whole action API is subject of change.
* @see https://www.drupal.org/project/drupal/issues/2011038
*/
final class ExtractTranscriptionText extends ActionBase implements ContainerFactoryPluginInterface {
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition): self {
return new self(
$configuration,
$plugin_id,
$plugin_definition,
);
}
/**
* {@inheritdoc}
*/
public function access($entity, AccountInterface $account = NULL, $return_as_object = FALSE): AccessResultInterface|bool {
/** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
$access = $entity->access('update', $account, TRUE);
return $return_as_object ? $access : $access->isAllowed();
}
/**
* {@inheritdoc}
*/
public function execute(ContentEntityInterface $entity = NULL): void {
if ($entity->bundle() == 'able_player_caption') {
$file = $entity->get('ableplayer_caption')->entity;
$uri = $file->getFileUri();
$contents = file_get_contents(\Drupal::service('file_system')->realpath($uri));
$entity->set('field_extracted_transcription', $contents);
$entity->save();
}
}
}