vlsuite-1.0.x-dev/modules/vlsuite_collection/src/VLSuiteCollectionHelper.php
modules/vlsuite_collection/src/VLSuiteCollectionHelper.php
<?php
namespace Drupal\vlsuite_collection;
use Drupal\Core\Extension\ExtensionPathResolver;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Serialization\Yaml;
use Drupal\layout_builder\Section;
use Drupal\layout_builder\SectionComponent;
use Drupal\section_library\Entity\SectionLibraryTemplate;
use Drupal\vlsuite_block\Entity\Bundle\VLSuiteBlockBase;
use Drupal\Core\File\FileSystemInterface;
use Drupal\file\FileRepositoryInterface;
/**
* Helper "VLSuiteCollectionHelper" service object.
*/
class VLSuiteCollectionHelper {
/**
* The extension path resolver.
*
* @var \Drupal\Core\Extension\ExtensionPathResolver
*/
protected $extensionPathResolver;
/**
* The entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* The file system.
*
* @var \Drupal\Core\File\FileSystemInterface
*/
protected $fileSystem;
/**
* The file repository.
*
* @var \Drupal\file\FileRepositoryInterface
*/
protected $fileRepository;
/**
* Constructs a "VLSuiteCollectionHelper" object.
*
* @param \Drupal\Core\Extension\ExtensionPathResolver $extension_path_resovler
* The extension path resolver.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager.
* @param \Drupal\Core\File\FileSystemInterface $file_sysyem
* The file system.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $file_repository
* The file repository.
*/
public function __construct(ExtensionPathResolver $extension_path_resovler, EntityTypeManagerInterface $entity_type_manager, FileSystemInterface $file_sysyem, FileRepositoryInterface $file_repository) {
$this->extensionPathResolver = $extension_path_resovler;
$this->entityTypeManager = $entity_type_manager;
$this->fileSystem = $file_sysyem;
$this->fileRepository = $file_repository;
}
/**
* Section library template generate Yml file.
*
* @param string $uuid
* Uuid to look for.
* @param string $module_name
* Module name where will be place inside assets folder.
*/
public function sectionLibraryTemplateGenerateYmlFile(string $uuid, string $module_name) {
$template = reset($this->entityTypeManager->getStorage('section_library_template')->loadByProperties(['uuid' => $uuid]));
if (empty($template)) {
return;
}
$template_values = [
'uuid' => $template->uuid(),
'label' => $template->label(),
'type' => 'template',
'layout_section' => [],
];
$sections = $template->get('layout_section')->getValue();
foreach ($sections as $section) {
$template_values['layout_section'][] = $section['section']->toArray();
}
$yml_path = $this->extensionPathResolver->getPath('module', $module_name) . '/assets/section-library-template-' . $template->uuid() . '.yml';
file_put_contents($yml_path, Yaml::encode($template_values));
}
/**
* Section library template create from Yml.
*
* @param string $uuid
* Section library template uuid to use.
* @param string $module_name
* Module name.
* @param string $media_image_uuid
* Media image uuid to use into "vlsuite_image" component fields.
*/
public function sectionLibraryTemplateCreateFromYmlFile(string $uuid, string $module_name, string $media_image_uuid) {
$yml_path = $this->extensionPathResolver->getPath('module', $module_name) . '/assets/section-library-template-' . $uuid . '.yml';
$yml_content = file_get_contents($yml_path);
if (!empty($this->entityTypeManager->getStorage('section_library_template')->loadByProperties(['uuid' => $uuid, 'type' => 'template'])) || !$yml_content) {
return;
}
$source = Yaml::decode($yml_content);
$new_template = [
'label' => $source['label'],
'type' => 'template',
'layout_section' => [],
];
foreach ($source['layout_section'] as $source) {
$components = [];
$section = $section = new Section(
$source['layout_id'],
$source['layout_settings'],
$components,
$source['third_party_settings'],
);
foreach ($source['components'] as $uuid => $component_array) {
$block = !empty($component_array['configuration']['block_serialized']) ? unserialize($component_array['configuration']['block_serialized']) : NULL;
if ($block instanceof VLSuiteBlockBase && $block->hasField('vlsuite_image') && !$block->get('vlsuite_image')->isEmpty()) {
$media_image_id = reset($this->entityTypeManager->getStorage('media')->loadByProperties(['uuid' => $media_image_uuid]))->id();
$block->set('vlsuite_image', ['target_id' => $media_image_id]);
$component_array['configuration']['block_serialized'] = !empty($media_image_id) ? serialize($block) : $component_array['configuration']['block_serialized'];
}
$section->appendComponent((new SectionComponent($uuid, $component_array['region'], $component_array['configuration'])));
}
$new_template['layout_section'][] = $section;
}
$new = SectionLibraryTemplate::create($new_template);
$new->save();
}
/**
* Generate media image.
*
* @param string $uuid
* Media uuid to use.
* @param string $module_name
* Module name.
* @param string $image_file_name
* Image file name.
* @param string $file_uuid
* Fille uuid to use.
*/
public function generateMediaImage(string $uuid, string $module_name, string $image_file_name, string $file_uuid) {
$image_file_source = $this->extensionPathResolver->getPath('module', $module_name) . '/assets/' . $image_file_name;
$destination = 'public://' . $module_name . '/';
if (empty($this->entityTypeManager->getStorage('file')->loadByProperties(['uuid' => $file_uuid]))) {
$image_content = file_get_contents($image_file_source);
if (!$image_content) {
return;
}
$this->fileSystem->prepareDirectory($destination, FileSystemInterface::CREATE_DIRECTORY | FileSystemInterface::MODIFY_PERMISSIONS);
$image_file = $this->fileRepository->writeData($image_content, $destination . $image_file_name, FileSystemInterface::EXISTS_REPLACE);
$image_file->set('uuid', $file_uuid);
$image_file->save();
}
else {
$image_file = reset($this->entityTypeManager->getStorage('file')->loadByProperties(['uuid' => $file_uuid]));
}
if (empty($this->entityTypeManager->getStorage('media')->loadByProperties(['uuid' => $uuid, 'bundle' => 'vlsuite_image']))) {
if (empty($this->entityTypeManager->getStorage('media_type')->load('vlsuite_image'))) {
// @see https://www.drupal.org/project/drupal/issues/3174255
return;
}
$this->entityTypeManager->getStorage('media')->create([
'name' => 'VLSuite collection placeholder image',
'bundle' => 'vlsuite_image',
'uuid' => $uuid,
'field_media_image' => [
'target_id' => $image_file->id(),
'alt' => 'VLSuite collection placeholder image',
],
])->save();
}
}
}
