openfed-8.x-8.5/openfed.install
openfed.install
<?php
/**
* @file
* Install, uninstall and update hooks for Openfed.
*/
use Drupal\Core\Config\Entity\ConfigEntityUpdater;
use Drupal\Core\DrupalKernelInterface;
use Drupal\field\FieldConfigInterface;
/**
* This will content management view, unsetting broken bulk actions.
*/
function openfed_update_101203() {
$default_admin_theme = 'openfed_admin';
// Make sure the theme is installed.
\Drupal::service('theme_installer')->install([$default_admin_theme]);
// Set it as the admin theme.
$config = \Drupal::service('config.factory')->getEditable('system.theme');
$config->set('admin', $default_admin_theme)->save();
}
/**
* Adds "File" entity type to the "enabled_entity_types" property in the
* "pathauto.settings.yml" configuration to fix File URL alias status issue.
*/
function openfed_update_101204() {
if (\Drupal::moduleHandler()->moduleExists('file_entity')) {
// Loads pathauto settings configuration.
$config = \Drupal::configFactory()->getEditable('pathauto.settings');
// Gets the pathauto enabled entity types.
$enabled_entity_types = $config->get('enabled_entity_types');
// If the File entity type is already enabled we do nothing.
if (in_array('file', $enabled_entity_types)) {
return;
}
// Adds the File entity to the array of enabled entity types.
$enabled_entity_types[] = 'file';
sort($enabled_entity_types);
// We update the configuration.
$config->set('enabled_entity_types', $enabled_entity_types);
$config->save();
}
}
/**
* Process allowed formats to provide a fix for the post update task
* allowed_formats_post_update_formats2core.
*
* This hook will provide a fix for Openfed 12.1.x projects which were using
* allowed_formats 2.x along with core allowed_formats. These projects may've
* been using core allowed_formats already and, without this update, the
* allowed_formats 3.x module will erase existing settings.
*
* This makes the allowed_formats_post_update_formats2core obsolete.
*/
function openfed_update_101205(&$sandbox) {
\Drupal::classResolver(ConfigEntityUpdater::class)->update($sandbox, 'field_config', function (FieldConfigInterface $field_config): bool {
// Can't use $field_config->getThirdPartySettings() because that doesn't
// reveal if allowed_formats module key exists. If the module key exists, even
// if it's empty, we want to remove it.
if (!array_key_exists('allowed_formats', $field_config->getSettings() ?? []) || !array_key_exists('allowed_formats', $field_config->get('third_party_settings') ?? [])) {
return FALSE;
}
// Get the core and contrib allowed_formats allowed formats.
$allowed_formats_contrib = array_values(array_filter($field_config->getThirdPartySetting('allowed_formats', 'allowed_formats', [])));
$allowed_formats_core = $field_config->getSetting('allowed_formats');
// Let's merge both contrib and core formats.
$allowed_formats = array_merge($allowed_formats_contrib, $allowed_formats_core);
// If there are core allowed formats, set the settings with it and unset the
// old third party settings value.
if ($allowed_formats && !empty($allowed_formats)) {
$field_config
->setSetting('allowed_formats', $allowed_formats)
->unsetThirdPartySetting('allowed_formats', 'allowed_formats');
return TRUE;
}
return FALSE;
});
}
/**
* Install twig_real_content module.
*/
function openfed_update_101206(&$sandbox) {
if (!\Drupal::moduleHandler()->moduleExists('twig_real_content')) {
$installer = \Drupal::service('module_installer');
$installer->install(['twig_real_content']);
}
}
/**
* Implements hook_update_dependencies().
*/
function openfed_update_dependencies() {
$dependencies = [];
if (Drupal::moduleHandler()->moduleExists('yoast_seo')) {
$dependencies['openfed'][120301] = [
'yoast_seo' => 8204,
];
}
return $dependencies;
}
/**
* Disable the yoast_seo auto refresh feature for existing websites.
*/
function openfed_update_120301(&$sandbox) {
if (!Drupal::moduleHandler()->moduleExists('yoast_seo')) {
return t('Yoast SEO is not enabled. No changes applied.');
}
Drupal::configFactory()->getEditable('yoast_seo.settings')
->set('auto_refresh_seo_result', FALSE)
->save();
$kernel = \Drupal::service('kernel');
assert($kernel instanceof DrupalKernelInterface);
$kernel->rebuildContainer();
return t('Yoast SEO auto refresh disabled.');
}
/**
* Update translatable entity reference fields to use the
* paragraphs_classic_asymmetric widget and enable the
* paragraphs_asymmetric_translation_widgets module if needed.
*
* This hook scans all translatable entity reference fields for the
* entity_reference_paragraphs widget in all form modes. If found, it enables the
* paragraphs_asymmetric_translation_widgets module and updates the widget type to
* paragraphs_classic_asymmetric for all form modes.
*
* @param array $sandbox
* An array used to store information between calls when the update is
* run in batches.
*
* @return string
* A string message listing all updated fields.
*/
function openfed_update_120500(&$sandbox) {
$entity_type_manager = \Drupal::entityTypeManager();
$content_translation_manager = \Drupal::service('content_translation.manager');
$needs_module = FALSE;
$form_display_updates = [];
$updated_fields = [];
$field_storage_configs = $entity_type_manager->getStorage('field_storage_config')->loadMultiple();
foreach ($field_storage_configs as $field_storage_config) {
$field_configs = $entity_type_manager->getStorage('field_config')->loadByProperties([
'field_name' => $field_storage_config->getName(),
]);
if (empty($field_configs)) {
continue;
}
foreach ($field_configs as $field_config) {
// Only process entity reference revision fields.
if ($field_config->getType() !== 'entity_reference_revisions') {
continue;
}
// Only process translatable fields.
if (!$field_config->isTranslatable()) {
continue;
}
// Only process fields referencing paragraphs.
if ($field_config->getFieldStorageDefinition()->getSetting('target_type') !== 'paragraph') {
continue;
}
// Check if content translation is enabled for this entity type and bundle.
// Skip when not enabled. The InlineParagraphsWidget::initIsTranslating
// method checks if the host entity is translatable.
$entity_type = $field_config->getTargetEntityTypeId();
$bundle = $field_config->getTargetBundle();
if (!$content_translation_manager->isEnabled($entity_type, $bundle)) {
continue;
}
$form_display_storage = $entity_type_manager->getStorage('entity_form_display');
$ids = $form_display_storage->getQuery()
->condition('targetEntityType', $entity_type)
->condition('bundle', $bundle)
->execute();
if (empty($ids)) {
continue;
}
$form_displays = $form_display_storage->loadMultiple($ids);
foreach ($form_displays as $form_display) {
$component = $form_display->getComponent($field_config->getName());
if (!$component || $component['type'] !== 'entity_reference_paragraphs') {
continue;
}
$needs_module = TRUE;
$key = $form_display->id();
if (!isset($form_display_updates[$key])) {
$form_display_updates[$key] = [
'fields' => [],
'entity_type' => $entity_type,
'bundle' => $bundle,
'mode' => $form_display->getMode(),
];
}
$form_display_updates[$key]['fields'][$field_config->getName()] = $component;
}
}
}
if ($needs_module && !\Drupal::moduleHandler()->moduleExists('paragraphs_asymmetric_translation_widgets')) {
\Drupal::service('module_installer')->install(['paragraphs_asymmetric_translation_widgets']);
}
foreach ($form_display_updates as $id => $item) {
$form_display = $entity_type_manager->getStorage('entity_form_display')->load($id);
foreach ($item['fields'] as $field_name => $component) {
$form_display->setComponent($field_name, [
'type' => 'paragraphs_classic_asymmetric',
] + $component);
$updated_fields[] = sprintf(
'Updated widget for field "%s" on entity "%s", bundle "%s", form mode "%s".',
$field_name,
$item['entity_type'],
$item['bundle'],
$item['mode']
);
}
$form_display->save();
}
if (!empty($updated_fields)) {
return "\n" . implode("\n", $updated_fields) . "\n";
}
return NULL;
}
