rocketship_paragraphs-5.0.0-alpha8/rocketship_paragraphs.install
rocketship_paragraphs.install
<?php
/**
* @file
* Install file.
*/
use Drupal\paragraphs\Entity\ParagraphsType;
use Drupal\file\Entity\File;
use Drupal\Core\File\FileSystemInterface;
use Symfony\Component\Yaml\Yaml;
/**
* Implements hook_install().
*/
function rocketship_paragraphs_install() {
$paragraphTypeFileNames = [
//'paragraphs.paragraphs_type.overview.yml',
'paragraphs.paragraphs_type.p_001.yml',
'paragraphs.paragraphs_type.p_002.yml',
'paragraphs.paragraphs_type.p_003.yml',
'paragraphs.paragraphs_type.p_004.yml',
'paragraphs.paragraphs_type.p_005.yml',
'paragraphs.paragraphs_type.p_006.yml',
'paragraphs.paragraphs_type.p_007.yml',
'paragraphs.paragraphs_type.p_007_child.yml',
'paragraphs.paragraphs_type.p_008.yml',
'paragraphs.paragraphs_type.p_009.yml',
'paragraphs.paragraphs_type.p_010.yml',
'paragraphs.paragraphs_type.p_010_child.yml',
'paragraphs.paragraphs_type.p_011.yml',
'paragraphs.paragraphs_type.p_012.yml',
'paragraphs.paragraphs_type.p_012_child.yml',
'paragraphs.paragraphs_type.p_014.yml',
'paragraphs.paragraphs_type.p_016.yml',
'paragraphs.paragraphs_type.p_017.yml',
'paragraphs.paragraphs_type.p_fblikebox.yml',
//'paragraphs.paragraphs_type.simple_header.yml',
];
$path = \Drupal::moduleHandler()
->getModule('rocketship_paragraphs')
->getPath();
// Prep file directory.
/** @var \Drupal\Core\File\FileSystemInterface $fileSystem */
$fileSystem = \Drupal::service('file_system');
$directory = 'public://paragraphs_type_icon';
$fileSystem->prepareDirectory($directory, FileSystemInterface::CREATE_DIRECTORY);
foreach ($paragraphTypeFileNames as $paragraphTypeFileName) {
// Read info from yaml file.
$paragraphTypeInfo = Yaml::parseFile("$path/config/install/$paragraphTypeFileName");
if (empty($paragraphTypeInfo['icon_uuid'])) {
continue;
}
$paragraphTypeId = $paragraphTypeInfo['id'];
$imageUuid = $paragraphTypeInfo['icon_uuid'];
$imageName = "$paragraphTypeId.svg";
$source = "$path/img/icons/$imageName";
$destination = "$directory/$imageName";
// Copy raw file.
$fileSystem->copy($source, $destination, FileSystemInterface::EXISTS_REPLACE);
// Create file entity.
$file = File::create([
'uri' => $destination,
]);
$file->setPermanent();
// Set UUID to match config.
$file->set('uuid', $imageUuid);
$file->save();
// Mark file as in use.
$file_usage = \Drupal::service('file.usage');
$file_usage->add($file, 'paragraphs', 'paragraphs_type', $paragraphTypeId);
}
// Make colorbox work on mobile.
\Drupal::configFactory()
->getEditable('colorbox.settings')
->set('advanced.mobile_detect', 0)
->save();
}
/**
* Enable module and grant permissions.
*/
function rocketship_paragraphs_update_8001() {
\Drupal::service('module_installer')
->install(['paragraphs_type_permissions']);
foreach (ParagraphsType::loadMultiple() as $type) {
rocketship_paragraphs_paragraphs_type_insert($type);
}
}
