Skip to content

Commit

Permalink
Adding update script
Browse files Browse the repository at this point in the history
  • Loading branch information
trackleft committed Aug 23, 2022
1 parent d6069d0 commit 032819c
Show file tree
Hide file tree
Showing 5 changed files with 297 additions and 98 deletions.
48 changes: 14 additions & 34 deletions config/schema/field_menu.schema.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,40 +5,12 @@ field_menu.settings:
menu_type_checkbox:
type: sequence
label: 'Available menus'
orderby: key
sequence:
type: string
type: system.menu.[%id]
label: 'Menu'
menu_type_checkbox_negate:
type: string
label: 'Negate the condition'

field.storage_settings.field_menu:
type: mapping
label: 'Menu field settings'

field.field_settings.field_menu:
type: mapping
label: 'Field menu settings'
mapping:
menu_type_checkbox:
type: sequence
label: 'Available menus'
sequence:
type: string
menu_type_checkbox_negate:
type: string
label: 'Negate the condition'

field_menu.settings:
type: mapping
label: 'Field menu settings'
mapping:
menu_type_checkbox:
type: sequence
label: 'Available menus'
sequence:
type: string
menu_type_checkbox_negate:
type: string
type: boolean
label: 'Negate the condition'

field.storage_settings.field_menu:
Expand All @@ -52,10 +24,12 @@ field.field_settings.field_menu:
menu_type_checkbox:
type: sequence
label: 'Available menus'
orderby: key
sequence:
type: string
type: system.menu.[%key]
label: 'Menus'
menu_type_checkbox_negate:
type: string
type: boolean
label: 'Negate the condition'

field.value.field_menu:
Expand Down Expand Up @@ -83,3 +57,9 @@ field.value.field_menu:
follow_parent:
type: string
label: 'Follow option'
parent:
type: string
label: 'Parent menu link'
render_parent:
type: boolean
label: 'Render parent menu link'
192 changes: 148 additions & 44 deletions field_menu.install
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,17 @@ use Drupal\Core\Database\Database;
*
* This affects all existing field_menu implementations on this site.
*/
function field_menu_update_9201() {
function field_menu_update_9232() {
$schema = Database::getConnection()->schema();
$database = \Drupal::database();

$all_field_menu_fields = \Drupal::service('entity_field.manager')->getFieldMapByFieldType('field_menu');
if (!$all_field_menu_fields) {
return t('No menu fields to update.');
};

foreach ($all_field_menu_fields as $entity_type => $field) {
$field_name = array_key_first($field);
// dd($entity_type);

// New column for menu title.
$spec_title = [
Expand Down Expand Up @@ -73,14 +78,34 @@ function field_menu_update_9201() {
'size' => 'tiny',
];
$schema->addField($entity_type . '__' . $field_name, $field_name . '_follow_parent', $spec_follow_parent);
// New column for fixed parent.
$spec_parent = [
'type' => 'text',
'description' => 'Fixed parent',
'not null' => FALSE,
'size' => 'tiny',
];
$schema->addField($entity_type . '__' . $field_name, $field_name . '_parent', $spec_parent);

// New column for render_parent.
$spec_render_parent = [
'type' => 'int',
'description' => 'Render parent menu link',
'not null' => FALSE,
'size' => 'tiny',
];
$schema->addField($entity_type . '__' . $field_name, $field_name . '_render_parent', $spec_render_parent);

// New revision column for menu title.
$spec_title = [
'type' => 'text',
'description' => 'Menu title',
'not null' => FALSE,
'size' => 'tiny',
'default' => 0,
];
$schema->addField($entity_type . '_revision__' . $field_name, $field_name . '_title', $spec_title);

// New revision column for selected menu.
$spec_menu = [
'type' => 'text',
Expand All @@ -89,6 +114,7 @@ function field_menu_update_9201() {
'size' => 'tiny',
];
$schema->addField($entity_type . '_revision__' . $field_name, $field_name . '_menu', $spec_menu);

// New revision column for starting level.
$spec_level = [
'type' => 'int',
Expand All @@ -97,6 +123,7 @@ function field_menu_update_9201() {
'size' => 'small',
];
$schema->addField($entity_type . '_revision__' . $field_name, $field_name . '_level', $spec_level);

// New revision column for depth.
$spec_depth = [
'type' => 'int',
Expand All @@ -105,6 +132,7 @@ function field_menu_update_9201() {
'size' => 'small',
];
$schema->addField($entity_type . '_revision__' . $field_name, $field_name . '_depth', $spec_depth);

// New revision column for expand all items.
$spec_expand_all_items = [
'type' => 'int',
Expand All @@ -129,49 +157,125 @@ function field_menu_update_9201() {
'size' => 'tiny',
];
$schema->addField($entity_type . '_revision__' . $field_name, $field_name . '_follow_parent', $spec_follow_parent);
}
}

/**
* Convert existing field_menu implementation to use new field properties.
*
* This affects all existing field_menu implementations on this site.
*/
function field_menu_update_9202(&$sandbox) {
// Need to figure out how to show a specific menu item and its children.
$existing_menu_setting = 'main:views_view:views.recipes.page_1';
$existing_include_root = TRUE;
$existing_menu_array = explode(':', $existing_menu_setting);
$existing_level = substr_count($existing_menu_setting, ':');
$menu_name = $existing_menu_array[0];
$depth = 0;
$expand_all_items = 1;
if ($existing_level) {
$new_level = $existing_level;
};
if ($existing_include_root) {
$new_level = $new_level - 1;
};
}
// New revision column for fixed parent.
$spec_parent = [
'type' => 'text',
'description' => 'Parent menu link',
'not null' => FALSE,
'size' => 'tiny',
'default' => 0,
];
$schema->addField($entity_type . '_revision__' . $field_name, $field_name . '_parent', $spec_parent);

/**
* Delete old unused field_menu properties.
*
* This affects all existing field_menu implementations on this site.
*/
function field_menu_update_9203(&$sandbox) {
$schema = Database::getConnection()->schema();
$all_field_menu_fields = \Drupal::service('entity_field.manager')->getFieldMapByFieldType('field_menu');
foreach ($all_field_menu_fields as $entity_type => $field) {
$field_name = array_key_first($field);
// After transforming all of the data, we can drop the old columns.
$schema->dropField($entity_type . '__' . $field_name, $field_name . '_menu_item_key');
$schema->dropField($entity_type . '__' . $field_name, $field_name . '_max_depth');
$schema->dropField($entity_type . '__' . $field_name, $field_name . '_include_root');
$schema->dropField($entity_type . '__' . $field_name, $field_name . '_menu_title');
$schema->dropField($entity_type . '_revision__' . $field_name, $field_name . '_menu_item_key');
$schema->dropField($entity_type . '_revision__' . $field_name, $field_name . '_max_depth');
$schema->dropField($entity_type . '_revision__' . $field_name, $field_name . '_include_root');
$schema->dropField($entity_type . '_revision__' . $field_name, $field_name . '_menu_title');
// New revision column for render_parent.
$spec_render_parent = [
'type' => 'int',
'description' => 'Render parent menu link',
'not null' => FALSE,
'size' => 'tiny',
];
$schema->addField($entity_type . '_revision__' . $field_name, $field_name . '_render_parent', $spec_render_parent);

// Modify field data.
$field_query = $database->select($entity_type . '__' . $field_name, 'f');
$field_query->fields('f', [
'entity_id',
'revision_id',
'delta',
'field_menu_menu_title',
'field_menu_menu_item_key',
'field_menu_max_depth',
'field_menu_include_root',
]);


$current_field_rows = $field_query->execute();
if (!is_null($field_query)) {
foreach ($current_field_rows as $row) {
$menu_levels_array = explode(':', $row->field_menu_menu_item_key);
$row->field_menu_menu = $menu_levels_array[0];
$field_update_query = $database->update($entity_type . '__' . $field_name);
$field_update_query->condition('entity_id', $row->entity_id);
$field_update_query->condition('revision_id', $row->revision_id);
$field_update_query->condition('delta', $row->delta);
$field_update_query->fields([
'field_menu_title' => $row->field_menu_menu_title,
'field_menu_menu' => $row->field_menu_menu,
'field_menu_level' => 1,
'field_menu_depth' => $row->field_menu_max_depth,
'field_menu_expand_all_items' => 1,
'field_menu_follow' => 0,
'field_menu_follow_parent' => '',
'field_menu_parent' => $row->field_menu_menu_item_key,
'field_menu_render_parent' => $row->field_menu_include_root,
]);
$field_update_query->execute();

}
}
// Revisions
$field_revision_query = $database->select($entity_type . '_revision__' . $field_name, 'fr');
$field_revision_query->fields('fr', [
'entity_id',
'revision_id',
'delta',
'field_menu_menu_title',
'field_menu_menu_item_key',
'field_menu_max_depth',
'field_menu_include_root',
]);
$current_field_revision_rows = $field_revision_query->execute();
if (!is_null($field_revision_query)) {
foreach ($current_field_revision_rows as $row) {
$menu_levels_array = explode(':', $row->field_menu_menu_item_key);
$row->field_menu_menu = $menu_levels_array[0];

$field_update_query = $database->update($entity_type . '_revision__' . $field_name);
$field_update_query->condition('entity_id', $row->entity_id);
$field_update_query->condition('revision_id', $row->revision_id);
$field_update_query->condition('delta', $row->delta);
$field_update_query->fields([
'field_menu_title' => $row->field_menu_menu_title,
'field_menu_menu' => $row->field_menu_menu,
'field_menu_level' => 1,
'field_menu_depth' => $row->field_menu_max_depth,
'field_menu_expand_all_items' => 1,
'field_menu_follow' => 0,
'field_menu_follow_parent' => '',
'field_menu_parent' => $row->field_menu_menu_item_key,
'field_menu_render_parent' => $row->field_menu_include_root,
]);
$field_update_query->execute();
}
// After transforming all of the data, we can drop the old columns.
$schema->dropField($entity_type . '__' . $field_name, $field_name . '_menu_item_key');
$schema->dropField($entity_type . '__' . $field_name, $field_name . '_max_depth');
$schema->dropField($entity_type . '__' . $field_name, $field_name . '_include_root');
$schema->dropField($entity_type . '__' . $field_name, $field_name . '_menu_title');
$schema->dropField($entity_type . '_revision__' . $field_name, $field_name . '_menu_item_key');
$schema->dropField($entity_type . '_revision__' . $field_name, $field_name . '_max_depth');
$schema->dropField($entity_type . '_revision__' . $field_name, $field_name . '_include_root');
$schema->dropField($entity_type . '_revision__' . $field_name, $field_name . '_menu_title');
}
// Current node field configurations
$field_manager = \Drupal::getContainer()->get('entity_field.manager');
// Because the manager was already loaded before the above config was forced,
// it will return the old configuration that was cached
$field_manager->clearCachedFieldDefinitions();
$field_storage_configs = $field_manager->getFieldStorageDefinitions($entity_type);

// Get the last installed manager, this is the gatekeeper that determines if
// an update is needed or can be done
$last_installed_repo = \Drupal::getContainer()->get('entity.last_installed_schema.repository');

// Get the last installed configurations for node fields
// These are iterative objects and need to stored as such, not just simple arrays,
// so reusing the previously set configs is not an option
$last_installed_configs = $last_installed_repo->getLastInstalledFieldStorageDefinitions($entity_type);

// Force the last installed config to be the current for the field
$last_installed_configs[$field_name] = $field_storage_configs[$field_name];
$last_installed_repo->setLastInstalledFieldStorageDefinitions($entity_type, $last_installed_configs);
}
}
Loading

0 comments on commit 032819c

Please sign in to comment.