Issue #2543726 by damiankloip, Wim Leers, claudiu.cristea, voleger, dagmar, dawehner, amateescu, tedbow, FredCorreia, mgalalm, caseylau, jibran, dbjpanda, pcambra, Grimreaper, idebr, dmouse, larowlan, Berdir, chx, andypost, yched, fgm: Make $term->parent behave like any other entity reference field, to fix REST and Migrate support and de-customize its Views integration
2018-01-15 23:40:26 +00:00
<?php
/**
* @file
* Install, update and uninstall functions for the taxonomy module.
*/
Issue #2981887 by amateescu, joachim, jibran, chr.fritsch, Manuel Garcia, timmillwood, plach, Wim Leers, Gábor Hojtsy, Berdir, jojototh, pameeela, dawehner, catch, Bojhan, Fabianx, Jo Fitzgerald: Add a publishing status to taxonomy terms
2018-07-27 09:09:33 +00:00
use Drupal\Core\Field\BaseFieldDefinition;
Issue #2543726 by damiankloip, Wim Leers, claudiu.cristea, voleger, dagmar, dawehner, amateescu, tedbow, FredCorreia, mgalalm, caseylau, jibran, dbjpanda, pcambra, Grimreaper, idebr, dmouse, larowlan, Berdir, chx, andypost, yched, fgm: Make $term->parent behave like any other entity reference field, to fix REST and Migrate support and de-customize its Views integration
2018-01-15 23:40:26 +00:00
/**
* Convert the custom taxonomy term hierarchy storage to a default storage.
*/
function taxonomy_update_8501() {
$definition_update_manager = \Drupal::entityDefinitionUpdateManager();
/** @var \Drupal\Core\Field\BaseFieldDefinition $field_storage_definition */
$field_storage_definition = $definition_update_manager->getFieldStorageDefinition('parent', 'taxonomy_term');
$field_storage_definition->setCustomStorage(FALSE);
$definition_update_manager->updateFieldStorageDefinition($field_storage_definition);
}
/**
* Copy hierarchy from {taxonomy_term_hierarchy} to {taxonomy_term__parent}.
*/
function taxonomy_update_8502(&$sandbox) {
$database = \Drupal::database();
if (!isset($sandbox['current'])) {
// Set batch ops sandbox.
$sandbox['current'] = 0;
$sandbox['max'] = $database->select('taxonomy_term_hierarchy')
->countQuery()
->execute()
->fetchField();
}
// Save the hierarchy.
$select = $database->select('taxonomy_term_hierarchy', 'h');
$select->join('taxonomy_term_data', 'd', 'h.tid = d.tid');
$hierarchy = $select
->fields('h', ['tid', 'parent'])
->fields('d', ['vid', 'langcode'])
->range($sandbox['current'], $sandbox['current'] + 100)
->execute()
->fetchAll();
// Restore data.
$insert = $database->insert('taxonomy_term__parent')
->fields(['bundle', 'entity_id', 'revision_id', 'langcode', 'delta', 'parent_target_id']);
$tid = -1;
foreach ($hierarchy as $row) {
if ($row->tid !== $tid) {
$delta = 0;
$tid = $row->tid;
}
$insert->values([
'bundle' => $row->vid,
'entity_id' => $row->tid,
'revision_id' => $row->tid,
'langcode' => $row->langcode,
'delta' => $delta,
'parent_target_id' => $row->parent,
]);
$delta++;
$sandbox['current']++;
}
$insert->execute();
$sandbox['#finished'] = empty($sandbox['max']) ? 1 : ($sandbox['current'] / $sandbox['max']);
if ($sandbox['#finished'] >= 1) {
// Update the entity type because the 'taxonomy_term_hierarchy' table is no
// longer part of its shared tables schema.
$definition_update_manager = \Drupal::entityDefinitionUpdateManager();
$definition_update_manager->updateEntityType($definition_update_manager->getEntityType('taxonomy_term'));
// \Drupal\Core\Entity\Sql\SqlContentEntityStorageSchema::onEntityTypeUpdate()
// only deletes *known* entity tables (i.e. the base, data and revision
// tables), so we have to drop it manually.
$database->schema()->dropTable('taxonomy_term_hierarchy');
return t('Taxonomy term hierarchy has been converted to default entity reference storage.');
}
}
/**
* Update views to use {taxonomy_term__parent} in relationships.
*/
function taxonomy_update_8503() {
$config_factory = \Drupal::configFactory();
foreach ($config_factory->listAll('views.view.') as $id) {
$view = $config_factory->getEditable($id);
foreach (array_keys($view->get('display')) as $display_id) {
$changed = FALSE;
foreach (['relationships', 'filters', 'arguments'] as $handler_type) {
$base_path = "display.$display_id.display_options.$handler_type";
$handlers = $view->get($base_path);
if (!$handlers) {
continue;
}
foreach ($handlers as $handler_key => $handler_config) {
$table_path = "$base_path.$handler_key.table";
$field_path = "$base_path.$handler_key.field";
$table = $view->get($table_path);
$field = $view->get($field_path);
if (($table && ($table === 'taxonomy_term_hierarchy')) && ($field && ($field === 'parent'))) {
$view->set($table_path, 'taxonomy_term__parent');
$view->set($field_path, 'parent_target_id');
$changed = TRUE;
}
}
}
if ($changed) {
$view->save(TRUE);
}
}
}
}
Issue #2981887 by amateescu, joachim, jibran, chr.fritsch, Manuel Garcia, timmillwood, plach, Wim Leers, Gábor Hojtsy, Berdir, jojototh, pameeela, dawehner, catch, Bojhan, Fabianx, Jo Fitzgerald: Add a publishing status to taxonomy terms
2018-07-27 09:09:33 +00:00
/**
* Add the publishing status fields to taxonomy terms.
*/
function taxonomy_update_8601() {
$definition_update_manager = \Drupal::entityDefinitionUpdateManager();
$entity_type = $definition_update_manager->getEntityType('taxonomy_term');
// Bail out early if a field named 'status' is already installed.
if ($definition_update_manager->getFieldStorageDefinition('status', 'taxonomy_term')) {
2018-08-09 21:10:38 +00:00
$message = \Drupal::state()->get('taxonomy_update_8601_skip_message', t('The publishing status field has <strong>not</strong> been added to taxonomy terms. See <a href=":link">this page</a> for more information on how to install it.', [
Issue #2981887 by amateescu, joachim, jibran, chr.fritsch, Manuel Garcia, timmillwood, plach, Wim Leers, Gábor Hojtsy, Berdir, jojototh, pameeela, dawehner, catch, Bojhan, Fabianx, Jo Fitzgerald: Add a publishing status to taxonomy terms
2018-07-27 09:09:33 +00:00
':link' => 'https://www.drupal.org/node/2985366',
2018-08-09 21:10:38 +00:00
]));
return $message;
Issue #2981887 by amateescu, joachim, jibran, chr.fritsch, Manuel Garcia, timmillwood, plach, Wim Leers, Gábor Hojtsy, Berdir, jojototh, pameeela, dawehner, catch, Bojhan, Fabianx, Jo Fitzgerald: Add a publishing status to taxonomy terms
2018-07-27 09:09:33 +00:00
}
// Add the 'published' entity key to the taxonomy_term entity type.
$entity_keys = $entity_type->getKeys();
$entity_keys['published'] = 'status';
$entity_type->set('entity_keys', $entity_keys);
$definition_update_manager->updateEntityType($entity_type);
// Add the status field.
$status = BaseFieldDefinition::create('boolean')
->setLabel(t('Publishing status'))
->setDescription(t('A boolean indicating the published state.'))
->setRevisionable(TRUE)
->setTranslatable(TRUE)
->setDefaultValue(TRUE);
$has_content_translation_status_field = \Drupal::moduleHandler()->moduleExists('content_translation') && $definition_update_manager->getFieldStorageDefinition('content_translation_status', 'taxonomy_term');
if ($has_content_translation_status_field) {
$status->setInitialValueFromField('content_translation_status', TRUE);
}
else {
$status->setInitialValue(TRUE);
}
$definition_update_manager->installFieldStorageDefinition('status', 'taxonomy_term', 'taxonomy_term', $status);
// Uninstall the 'content_translation_status' field if needed.
if ($has_content_translation_status_field) {
$content_translation_status = $definition_update_manager->getFieldStorageDefinition('content_translation_status', 'taxonomy_term');
$definition_update_manager->uninstallFieldStorageDefinition($content_translation_status);
}
return t('The publishing status field has been added to taxonomy terms.');
}