
| Current Path : /var/www/html/strat/web/modules/contrib/metatag/ |
Linux ift1.ift-informatik.de 5.4.0-216-generic #236-Ubuntu SMP Fri Apr 11 19:53:21 UTC 2025 x86_64 |
| Current File : /var/www/html/strat/web/modules/contrib/metatag/metatag.install |
<?php
/**
* @file
* Requirements and update functions for the Metatag module.
*/
use Drupal\Core\StringTranslation\TranslatableMarkup;
/**
* Implements hook_requirements().
*/
function metatag_requirements($phase) {
$requirements = [];
if ($phase == 'runtime') {
// Note that no meta tags will be output while the site is in maintenance
// mode.
if (\Drupal::state()->get('system.maintenance_mode')) {
$requirements['metatag_maintenance_mode'] = [
'severity' => REQUIREMENT_WARNING,
'title' => 'Metatag',
'value' => new TranslatableMarkup('Not enabled while in maintenance mode'),
'description' => new TranslatableMarkup('Please note that while the site is in maintenance mode none of the usual meta tags will be output.'),
];
}
// Recommend the Schema.org Metatag module.
if (!\Drupal::moduleHandler()->moduleExists('schema_metatag')) {
$requirements['metatag_schema'] = [
'severity' => REQUIREMENT_INFO,
'title' => 'Metatag',
'value' => new TranslatableMarkup('Schema.org Metatag is recommended'),
'description' => new TranslatableMarkup('The <a href="@module">Schema.org Metatag</a> module is highly recommended to add <a href="@jsonld">JSON-LD</a> -formatted <a href="@schema">schema.org</a> compatible data structures to the site.', [
'@module' => 'https://www.drupal.org/project/schema_metatag',
'@jsonld' => 'https://json-ld.org',
'@schema' => 'https://schema.org',
]),
];
}
else {
$requirements['metatag_schema'] = [
'severity' => REQUIREMENT_OK,
'title' => 'Metatag',
'value' => new TranslatableMarkup('Schema.org Metatag is installed'),
'description' => new TranslatableMarkup('The <a href="@module">Schema.org Metatag</a> module is installed.', [
'@module' => 'https://www.drupal.org/project/schema_metatag',
]),
];
}
// Suggest the Token OR module.
if (!\Drupal::moduleHandler()->moduleExists('token_or')) {
$requirements['metatag_token_or'] = [
'severity' => REQUIREMENT_INFO,
'title' => 'Metatag',
'value' => new TranslatableMarkup('Token Or'),
'description' => new TranslatableMarkup('The <a href="@module">Token OR module</a> is suggested for when the basic content tokens are not flexible enough, e.g. to make a meta tag show the contents of one field if it is filled in or another if the first one is empty.', [
'@module' => 'https://www.drupal.org/project/token_or',
]),
];
}
}
return $requirements;
}
/**
* Implements hook_update_last_removed().
*/
function metatag_update_last_removed() {
return 8106;
}
/**
* Enable the new metatag_open_graph module.
*/
function metatag_update_8107() {
\Drupal::service('module_installer')->install(['metatag_open_graph']);
return (string) new TranslatableMarkup("The new Metatag: Open Graph module has been enabled.");
}
/**
* Need to clear caches after updating from 8.x-1.12.
*/
function metatag_update_8108() {
return (string) new TranslatableMarkup("The sites's caches will need to be rebuild to ensure Metatag works as intended.");
}
/**
* Update mask_icon values to the new structure.
*/
function metatag_update_8109(&$sandbox) {
// This whole top section only needs to be done the first time.
if (!isset($sandbox['records_processed'])) {
$sandbox['records_processed'] = 0;
$sandbox['total_records'] = 0;
$sandbox['current_field'] = 0;
$sandbox['current_record'] = 0;
// Counter to enumerate the fields so we can access them in the array
// by number rather than name.
$field_counter = 0;
// Get all of the field storage entities of type metatag.
/** @var \Drupal\field\FieldStorageConfigInterface[] $field_storage_configs */
$field_storage_configs = \Drupal::entityTypeManager()
->getStorage('field_storage_config')
->loadByProperties(['type' => 'metatag']);
foreach ($field_storage_configs as $field_storage) {
$field_name = $field_storage->getName();
// Get the individual fields (field instances) associated with bundles.
$fields = \Drupal::entityTypeManager()
->getStorage('field_config')
->loadByProperties([
'field_name' => $field_name,
'entity_type' => $field_storage->getTargetEntityTypeId(),
]);
foreach ($fields as $field) {
// Get the bundle this field is attached to.
$bundle = $field->getTargetBundle();
// Determine the table and "value" field names.
// @todo The class path to getTableMapping() seems to be invalid?
/** @var \Drupal\Core\Entity\Sql\DefaultTableMapping $table_mapping */
$table_mapping = \Drupal::entityTypeManager()
->getStorage($field->getTargetEntityTypeId())
->getTableMapping();
$field_table = $table_mapping->getFieldTableName($field_name);
$field_value_field = $table_mapping->getFieldColumnName($field_storage, 'value');
// Get all records where the field data does not match the default.
$query = \Drupal::database()->select($field_table);
$query->addField($field_table, 'entity_id');
$query->addField($field_table, 'revision_id');
$query->addField($field_table, 'langcode');
$query->addField($field_table, $field_value_field);
$query->condition('bundle', $bundle, '=');
$result = $query->execute();
$records = $result->fetchAll();
if (empty($records)) {
continue;
}
// Fill in all the sandbox information so we can batch the individual
// record comparing and updating.
$sandbox['fields'][$field_counter]['field_table'] = $field_table;
$sandbox['fields'][$field_counter]['field_value_field'] = $field_value_field;
$sandbox['fields'][$field_counter]['records'] = $records;
$sandbox['total_records'] += count($sandbox['fields'][$field_counter]['records']);
$field_counter++;
}
}
}
if ($sandbox['total_records'] == 0) {
// No partially overridden fields so we can skip the whole batch process.
$sandbox['#finished'] = 1;
}
else {
// Begin the batch processing of individual field records.
$max_per_batch = 10;
$counter = 1;
$current_field = $sandbox['current_field'];
$current_field_records = $sandbox['fields'][$current_field]['records'];
$current_record = $sandbox['current_record'];
$field_table = $sandbox['fields'][$current_field]['field_table'];
$field_value_field = $sandbox['fields'][$current_field]['field_value_field'];
// Loop through the field(s) and update the mask_icon values if necessary.
while ($counter <= $max_per_batch && isset($current_field_records[$current_record])) {
$record = $current_field_records[$current_record];
// Strip any empty tags or ones matching the field's defaults and leave
// only the overridden tags in $new_tags.
$tags = unserialize($record->$field_value_field, ['allowed_classes' => FALSE]);
if (isset($tags['mask-icon'])) {
$tags['mask_icon'] = [
'href' => $tags['mask-icon'],
];
$tags_string = serialize($tags);
\Drupal::database()->update($field_table)
->fields([
$field_value_field => $tags_string,
])
->condition('entity_id', $record->entity_id)
->condition('revision_id', $record->revision_id)
->condition('langcode', $record->langcode)
->execute();
}
$counter++;
$current_record++;
}
// We ran out of records for the field so start the next batch out with the
// next field.
if (!isset($current_field_records[$current_record])) {
$current_field++;
$current_record = 0;
}
// We have finished all the fields. All done.
if (!isset($sandbox['fields'][$current_field])) {
$sandbox['records_processed'] += $counter - 1;
$sandbox['#finished'] = 1;
}
// Update the sandbox values to prepare for the next round.
else {
$sandbox['current_field'] = $current_field;
$sandbox['current_record'] = $current_record;
$sandbox['records_processed'] += $counter - 1;
$sandbox['#finished'] = $sandbox['records_processed'] / $sandbox['total_records'];
}
}
if ($sandbox['total_records'] > 0) {
return (string) t('Processed @processed of @total overridden Metatag records.', [
'@processed' => $sandbox['records_processed'],
'@total' => $sandbox['total_records'],
]);
}
else {
return (string) t("There were no overridden Metatag records.");
}
}
/**
* Sets tag_trim_end standard settings for existing installations.
*/
function metatag_update_9401() {
$config_factory = \Drupal::configFactory();
$config = $config_factory->getEditable('metatag.settings');
$config->set('tag_trim_end', "|.,-:;/+&([{\"'");
$config->save();
}