vbp_text_and_image.install in Varbase Bootstrap Paragraphs 9.0.x
Install, uninstall and update hooks for VBP text and image module.
File
modules/vbp_text_and_image/vbp_text_and_image.installView source
<?php
/**
* @file
* Install, uninstall and update hooks for VBP text and image module.
*/
use Symfony\Component\Yaml\Yaml;
use Drupal\Core\Config\InstallStorage;
use Drupal\Core\Config\StorageInterface;
use Drupal\Core\Config\FileStorage;
use Vardot\Entity\EntityDefinitionUpdateManager;
/**
* Implements hook_install().
*/
function vbp_text_and_image_install() {
$module_name = preg_replace('/_install$/', '', __FUNCTION__);
$module_path = Drupal::service('module_handler')
->getModule($module_name)
->getPath();
// Processer for install: in [$module_name].info.yml file.
// --------------------------------------------------------------------------.
$module_info_file = $module_path . '/' . $module_name . '.info.yml';
if (file_exists($module_info_file)) {
$module_info_data = (array) Yaml::parse(file_get_contents($module_info_file));
if (isset($module_info_data['install']) && is_array($module_info_data['install'])) {
\Drupal::service('module_installer')
->install($module_info_data['install'], TRUE);
}
}
// Install optional configs.
$optional_install_path = $module_path . '/' . InstallStorage::CONFIG_OPTIONAL_DIRECTORY;
if (is_dir($optional_install_path)) {
$config_installer = \Drupal::service('config.installer');
$config_installer
->installDefaultConfig('module', $module_name);
// Create field storage configs first in active config.
$storage_config_files = \Drupal::service('file_system')
->scanDirectory($optional_install_path, '/^field.storage.*\\.(yml)$/i');
if (isset($storage_config_files) && is_array($storage_config_files)) {
foreach ($storage_config_files as $storage_config_file) {
$storage_config_file_content = file_get_contents(DRUPAL_ROOT . '/' . $storage_config_file->uri);
$storage_config_file_data = (array) Yaml::parse($storage_config_file_content);
$config_factory = \Drupal::configFactory()
->getEditable($storage_config_file->name);
$config_factory
->setData($storage_config_file_data)
->save(TRUE);
}
}
// Install any optional config the module provides.
$storage = new FileStorage($optional_install_path, StorageInterface::DEFAULT_COLLECTION);
$config_installer
->installOptionalConfig($storage, '');
// Have the .settings.yml configs into the active config.
$settings_config_files = \Drupal::service('file_system')
->scanDirectory($optional_install_path, '/^.*\\.(settings.yml)$/i');
if (isset($settings_config_files) && is_array($settings_config_files)) {
foreach ($settings_config_files as $settings_config_file) {
$settings_config_file_content = file_get_contents(DRUPAL_ROOT . '/' . $settings_config_file->uri);
$settings_config_file_data = (array) Yaml::parse($settings_config_file_content);
$config_factory = \Drupal::configFactory()
->getEditable($settings_config_file->name);
$config_factory
->setData($settings_config_file_data)
->save(TRUE);
}
}
}
// ---------------------------------------------------------------------------
// Add target bundle [text and image] to landing page components
// (landing page paragraphs field) without changing old configurations.
_add_target_bundle_text_and_image_to_landing_page_components();
// Entity updates to clear up any mismatched entity and/or field definitions
// And Fix changes were detected in the entity type and field definitions.
\Drupal::classResolver()
->getInstanceFromDefinition(EntityDefinitionUpdateManager::class)
->applyUpdates();
}
/**
* Add target bundle [text and image] to landing page components.
*
* (landing page paragraphs field) without changing old configurations.
*/
function _add_target_bundle_text_and_image_to_landing_page_components() {
$site_config = \Drupal::configFactory()
->getEditable('field.field.node.landing_page.field_lp_paragraphs');
$config_data = $site_config
->get();
/* dependencies:
* config:
* - paragraphs.paragraphs_type.text_and_image
*/
if (!isset($config_data['dependencies']['config']['paragraphs.paragraphs_type.text_and_image'])) {
$config_data['dependencies']['config'][] = 'paragraphs.paragraphs_type.text_and_image';
}
/* settings:
* handler_settings:
* target_bundles:
* text_and_image: text_and_image
*/
if (!isset($config_data['settings']['handler_settings']['target_bundles']['text_and_image'])) {
$config_data['settings']['handler_settings']['target_bundles']['text_and_image'] = 'text_and_image';
}
/* target_bundles_drag_drop
* text_and_image:
* enabled: true
* weight: 32
*/
if (!isset($config_data['settings']['handler_settings']['target_bundles_drag_drop']['text_and_image'])) {
$config_data['settings']['handler_settings']['target_bundles_drag_drop']['text_and_image'] = [
'enabled' => TRUE,
'weight' => 32,
];
}
$site_config
->setData($config_data)
->save(TRUE);
}
/**
* Issue #2904173: Changed [Background Color] in the Styling Settings to use.
*
* Use a set of Color set to choose with [Color boxes] with a general
* color settings.
*/
function vbp_text_and_image_update_8001() {
$module_name = preg_replace('/_install$/', '', __FUNCTION__);
$module_path = Drupal::service('module_handler')
->getModule($module_name)
->getPath();
$optional_install_path = $module_path . '/' . InstallStorage::CONFIG_OPTIONAL_DIRECTORY;
$configs['core.entity_form_display.paragraph.text_and_image.default'] = $optional_install_path . 'core.entity_form_display.paragraph.text_and_image.default.yml';
foreach ($configs as $name => $config_path) {
$data = (array) Yaml::parse($config_path);
$config = \Drupal::configFactory()
->getEditable($name);
$config
->setData($data)
->save(TRUE);
}
}
/**
* Issue #3025036: Add target bundle [text and image] to landing page.
*
* Landing page components (landing page paragraphs field) without changing old
* configurations.
*/
function vbp_text_and_image_update_8602() {
_add_target_bundle_text_and_image_to_landing_page_components();
}
Functions
Name | Description |
---|---|
vbp_text_and_image_install | Implements hook_install(). |
vbp_text_and_image_update_8001 | Issue #2904173: Changed [Background Color] in the Styling Settings to use. |
vbp_text_and_image_update_8602 | Issue #3025036: Add target bundle [text and image] to landing page. |
_add_target_bundle_text_and_image_to_landing_page_components | Add target bundle [text and image] to landing page components. |