You are here

flag_lists.install in Flag Lists 8

Contains install and updates for Flag Lists module.

File

flag_lists.install
View source
<?php

/**
 * @file
 * Contains install and updates for Flag Lists module.
 */
use Drupal\Core\Field\BaseFieldDefinition;

/**
 * Change the name of bundle to type in flag_list_item.
 */
function flag_lists_update_8001() {
  $entity_type_manager = \Drupal::entityTypeManager();
  $bundle_of = 'flag_list_item';
  $storage = $entity_type_manager
    ->getStorage($bundle_of);
  $bundle_definition = $entity_type_manager
    ->getDefinition($bundle_of);

  // Sometimes the primary key isn't 'id'. e.g. 'eid' or 'item_id'.
  $id_key = $bundle_definition
    ->getKey('id');

  // If there is no data table defined then use the base table.
  $table_name = $storage
    ->getDataTable() ?: $storage
    ->getBaseTable();
  $database = \Drupal::database();
  $definition_manager = \Drupal::entityDefinitionUpdateManager();

  // Store the existing values.
  $type_values = $database
    ->select($table_name)
    ->fields($table_name, [
    $id_key,
    'bundle',
  ])
    ->execute()
    ->fetchAllKeyed();

  // Clear out the values.
  $database
    ->update($table_name)
    ->fields([
    'bundle' => NULL,
  ])
    ->execute();

  // Uninstall the field.
  $field_storage_definition = $definition_manager
    ->getFieldStorageDefinition('bundle', $bundle_of);
  $definition_manager
    ->uninstallFieldStorageDefinition($field_storage_definition);

  // Create a new field definition.
  $new_type_field = BaseFieldDefinition::create('string')
    ->setLabel(t('Entity type'))
    ->setDescription(t('The entity type.'))
    ->setDefaultValue('no')
    ->setRevisionable(FALSE)
    ->setTranslatable(FALSE);

  // Install the new definition.
  $definition_manager
    ->installFieldStorageDefinition('type', $bundle_of, $bundle_of, $new_type_field);
  foreach ($type_values as $id => $value) {
    $database
      ->update($table_name)
      ->fields([
      'type' => $value,
    ])
      ->condition($id_key, $id)
      ->execute();
  }
  $message = "Converted Flag List Item table from 'bundle' to 'type'.";
  return $message;
}

/**
 * Update all views to use the updated Flag List Item table info.
 */
function flag_lists_update_8002() {
  $config_factory = \Drupal::configFactory();

  // Find all views configs.
  foreach ($config_factory
    ->listAll('views.view.') as $view_config_name) {
    $view = $config_factory
      ->getEditable($view_config_name);

    // Go through each display on each view.
    $displays = $view
      ->get('display');
    foreach ($displays as $display_name => $display) {

      // Go through all the entity fields on each display and
      // find ones currently using 'bundle' as the table.
      if (!empty($display['display_options']['fields'])) {
        foreach ($display['display_options']['fields'] as $field_name => $field) {
          if (isset($field['entity_type']) && $field['entity_type'] === 'flag_list_item' && $field['entity_field'] === 'bundle') {
            $base = "display.{$display_name}.display_options.fields.{$field_name}";

            // Update the fields to use the updated table.
            $view
              ->set($base . '.id', 'type');
            $view
              ->set($base . '.field', 'type');
            $view
              ->set($base . '.label', 'Entity Type');
            $view
              ->set($base . '.entity_field', 'type');
            $type = $view
              ->get($base);
            $view
              ->clear($base);
            $base = "display.{$display_name}.display_options.fields.type";
            $view
              ->set($base, $type);
          }
        }
      }
    }
    $view
      ->save(TRUE);
  }
  $message = 'Update all views to use updated Flag List Items. Please review the My Flag Lists Items view as it has been tweaked.';
  return $message;
}

/**
 * Update the flag list template to include token for flagging collection names.
 */
function flag_lists_update_8003() {
  $config_factory = \Drupal::configFactory();
  $config = $config_factory
    ->getEditable('flag.flag.flag_list_template_1');
  $config
    ->set('flag_short', 'Add this item to the [flagging_collection:title] list');
  $config
    ->set('flag_long', 'Add this to your flag list');
  $config
    ->set('flag_message', 'Added to the flag list');
  $config
    ->set('unflag_short', 'Remove this item from the [flagging_collection:title] list');
  $config
    ->set('unflag_long', 'Remove this item from your flag list');
  $config
    ->set('unflag_message', 'Removed from the flag list');
  $config
    ->save(TRUE);
  $message = 'Updated the flag list template with tokens. Please review the your other flag list related flags as well.';
  return $message;
}

Functions

Namesort descending Description
flag_lists_update_8001 Change the name of bundle to type in flag_list_item.
flag_lists_update_8002 Update all views to use the updated Flag List Item table info.
flag_lists_update_8003 Update the flag list template to include token for flagging collection names.