Loading modal_page.services.yml +1 −1 Original line number Diff line number Diff line services: modal_page.modals: class: Drupal\modal_page\Service\ModalPageService arguments: ['@language_manager', '@entity_type.manager', '@config.factory', '@database', '@request_stack', '@path.matcher', '@uuid', '@current_user', '@path_alias.manager', '@module_handler', '@path.current'] arguments: ['@language_manager', '@entity_type.manager', '@config.factory', '@database', '@request_stack', '@path.matcher', '@uuid', '@current_user', '@path_alias.manager', '@module_handler', '@path.current', '@entity.repository'] modal_page.helper: class: Drupal\modal_page\Service\ModalPageHelperService arguments: ['@language_manager', '@current_user', '@entity_type.manager'] src/Form/ModalForm.php +5 −0 Original line number Diff line number Diff line Loading @@ -1066,6 +1066,11 @@ class ModalForm extends EntityForm { $modal->setDontShowAgainLabel((string) $this->t("Don't show again")); } // Save inline image. $text = $form_state->getValue('body')['value']; $uuids = $this->modalPageService->extractFilesUuid($text); $this->modalPageService->recordFileUsage($uuids); // Modal Save. $status = $modal->save(); Loading src/Service/ModalPageService.php +57 −1 Original line number Diff line number Diff line Loading @@ -16,6 +16,8 @@ use Drupal\Component\Utility\Html; use Drupal\path_alias\AliasManagerInterface; use Drupal\Core\Extension\ModuleHandlerInterface; use Drupal\Core\Path\CurrentPathStack; use Drupal\Core\Entity\EntityRepositoryInterface; use Drupal\Core\Entity\EntityStorageException; /** * Modal Page Service Class. Loading Loading @@ -101,10 +103,17 @@ class ModalPageService { */ protected $projectHandler; /** * Drupal\Core\Entity\EntityRepositoryInterface service. * * @var \Drupal\Core\Entity\EntityRepositoryInterface */ protected $entityRepository; /** * Construct of Modal Page service. */ public function __construct(LanguageManagerInterface $language_manager, EntityTypeManagerInterface $entity_manager, ConfigFactoryInterface $config_factory, Connection $database, RequestStack $request_stack, PathMatcherInterface $path_matcher, UuidInterface $uuid_service, AccountProxyInterface $current_user, AliasManagerInterface $alias_manager, ModuleHandlerInterface $project_handler, CurrentPathStack $current_path) { public function __construct(LanguageManagerInterface $language_manager, EntityTypeManagerInterface $entity_manager, ConfigFactoryInterface $config_factory, Connection $database, RequestStack $request_stack, PathMatcherInterface $path_matcher, UuidInterface $uuid_service, AccountProxyInterface $current_user, AliasManagerInterface $alias_manager, ModuleHandlerInterface $project_handler, CurrentPathStack $current_path, EntityRepositoryInterface $entityRepository) { $this->languageManager = $language_manager; $this->entityTypeManager = $entity_manager; $this->pathMatcher = $path_matcher; Loading @@ -116,6 +125,7 @@ class ModalPageService { $this->aliasManager = $alias_manager; $this->projectHandler = $project_handler; $this->currentPath = $current_path; $this->entityRepository = $entityRepository; } /** Loading Loading @@ -599,4 +609,50 @@ class ModalPageService { return $class; } /** * Parse an HTML snippet for any linked file with data-entity-uuid attributes. * * @param string $text * The partial (X)HTML snippet to load. Invalid markup will be corrected on * import. * * @return array * An array of all found UUIDs. */ public function extractFilesUuid($text) { $dom = Html::load($text); $xpath = new \DOMXPath($dom); $uuids = []; foreach ($xpath->query('//*[@data-entity-type="file" and @data-entity-uuid]') as $file) { $uuids[] = $file->getAttribute('data-entity-uuid'); } return $uuids; } /** * Records file usage of files referenced by formatted text fields. * * Every referenced file that does not yet have the FILE_STATUS_PERMANENT * state, will be given that state. * * @param array $uuids * An array of file entity UUIDs. */ public function recordFileUsage(array $uuids) { try { foreach ($uuids as $uuid) { if ($file = $this->entityRepository->loadEntityByUuid('file', $uuid)) { if ($file->status !== FILE_STATUS_PERMANENT) { $file->status = FILE_STATUS_PERMANENT; $file->save(); } } } } catch (EntityStorageException $exception) { $this->logger('modal_page')->warning($exception->getMessage()); } } } Loading
modal_page.services.yml +1 −1 Original line number Diff line number Diff line services: modal_page.modals: class: Drupal\modal_page\Service\ModalPageService arguments: ['@language_manager', '@entity_type.manager', '@config.factory', '@database', '@request_stack', '@path.matcher', '@uuid', '@current_user', '@path_alias.manager', '@module_handler', '@path.current'] arguments: ['@language_manager', '@entity_type.manager', '@config.factory', '@database', '@request_stack', '@path.matcher', '@uuid', '@current_user', '@path_alias.manager', '@module_handler', '@path.current', '@entity.repository'] modal_page.helper: class: Drupal\modal_page\Service\ModalPageHelperService arguments: ['@language_manager', '@current_user', '@entity_type.manager']
src/Form/ModalForm.php +5 −0 Original line number Diff line number Diff line Loading @@ -1066,6 +1066,11 @@ class ModalForm extends EntityForm { $modal->setDontShowAgainLabel((string) $this->t("Don't show again")); } // Save inline image. $text = $form_state->getValue('body')['value']; $uuids = $this->modalPageService->extractFilesUuid($text); $this->modalPageService->recordFileUsage($uuids); // Modal Save. $status = $modal->save(); Loading
src/Service/ModalPageService.php +57 −1 Original line number Diff line number Diff line Loading @@ -16,6 +16,8 @@ use Drupal\Component\Utility\Html; use Drupal\path_alias\AliasManagerInterface; use Drupal\Core\Extension\ModuleHandlerInterface; use Drupal\Core\Path\CurrentPathStack; use Drupal\Core\Entity\EntityRepositoryInterface; use Drupal\Core\Entity\EntityStorageException; /** * Modal Page Service Class. Loading Loading @@ -101,10 +103,17 @@ class ModalPageService { */ protected $projectHandler; /** * Drupal\Core\Entity\EntityRepositoryInterface service. * * @var \Drupal\Core\Entity\EntityRepositoryInterface */ protected $entityRepository; /** * Construct of Modal Page service. */ public function __construct(LanguageManagerInterface $language_manager, EntityTypeManagerInterface $entity_manager, ConfigFactoryInterface $config_factory, Connection $database, RequestStack $request_stack, PathMatcherInterface $path_matcher, UuidInterface $uuid_service, AccountProxyInterface $current_user, AliasManagerInterface $alias_manager, ModuleHandlerInterface $project_handler, CurrentPathStack $current_path) { public function __construct(LanguageManagerInterface $language_manager, EntityTypeManagerInterface $entity_manager, ConfigFactoryInterface $config_factory, Connection $database, RequestStack $request_stack, PathMatcherInterface $path_matcher, UuidInterface $uuid_service, AccountProxyInterface $current_user, AliasManagerInterface $alias_manager, ModuleHandlerInterface $project_handler, CurrentPathStack $current_path, EntityRepositoryInterface $entityRepository) { $this->languageManager = $language_manager; $this->entityTypeManager = $entity_manager; $this->pathMatcher = $path_matcher; Loading @@ -116,6 +125,7 @@ class ModalPageService { $this->aliasManager = $alias_manager; $this->projectHandler = $project_handler; $this->currentPath = $current_path; $this->entityRepository = $entityRepository; } /** Loading Loading @@ -599,4 +609,50 @@ class ModalPageService { return $class; } /** * Parse an HTML snippet for any linked file with data-entity-uuid attributes. * * @param string $text * The partial (X)HTML snippet to load. Invalid markup will be corrected on * import. * * @return array * An array of all found UUIDs. */ public function extractFilesUuid($text) { $dom = Html::load($text); $xpath = new \DOMXPath($dom); $uuids = []; foreach ($xpath->query('//*[@data-entity-type="file" and @data-entity-uuid]') as $file) { $uuids[] = $file->getAttribute('data-entity-uuid'); } return $uuids; } /** * Records file usage of files referenced by formatted text fields. * * Every referenced file that does not yet have the FILE_STATUS_PERMANENT * state, will be given that state. * * @param array $uuids * An array of file entity UUIDs. */ public function recordFileUsage(array $uuids) { try { foreach ($uuids as $uuid) { if ($file = $this->entityRepository->loadEntityByUuid('file', $uuid)) { if ($file->status !== FILE_STATUS_PERMANENT) { $file->status = FILE_STATUS_PERMANENT; $file->save(); } } } } catch (EntityStorageException $exception) { $this->logger('modal_page')->warning($exception->getMessage()); } } }