flag_lists-4.0.x-dev/flag_lists.install

flag_lists.install
<?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;
}

/**
 * Update the Related flag to share the new name.
 */
function flag_lists_update_8004() {

  $missingRelated = FALSE;
  $flagService = \Drupal::service('flag');
  $flagListsService = \Drupal::service('flaglists');
  $database = \Drupal::database();

  foreach ($flagListsService->getAllFlaggingCollections() as $flagList) {
    $entity_id = $flagList->id();
    $userId = $flagList->getOwnerId();
    if (empty($flagList->getRelatedFlag())) {
      // The related Flag is broken, just bail out.
      // It must be fixed by the method recommended in the help.
      // Show a message that the related Flag is missing.
      $missingRelated = TRUE;
      continue;
    }
    $relatedFlag = $flagList->getRelatedFlag();
    $originalId = $relatedFlag->id();
    $newName = 'relflag_' . $entity_id . '_' . $userId;
    if ($originalId <> $newName) {
      $database->update('flagging')
        ->fields(['flag_id' => $newName])
        ->condition('flag_id', $originalId)
        ->execute();
      $database->update('flag_counts')
        ->fields(['flag_id' => $newName])
        ->condition('flag_id', $originalId)
        ->execute();
      $database->update('flagging_collection_field_data')
        ->fields(['relatedflag' => $newName])
        ->condition('id', $entity_id)
        ->execute();
      $database->update('flagging_collection_field_revision')
        ->fields(['relatedflag' => $newName])
        ->condition('id', $entity_id)
        ->execute();
      $database->update('flag_list_item_field_data')
        ->fields(['baseflag' => $newName])
        ->condition('baseflag', $originalId)
        ->execute();
      // Update the name of the Related Flag by creating a new
      // and delete the old. The deletion must be done after all
      // other updates, if not much more is deleted than expected!
      $newRelatedFlag = $relatedFlag->createDuplicate();
      $newRelatedFlag->set('id', $newName);
      $relatedFlag->delete();
      $newRelatedFlag->save();
    }
  }
  if ($missingRelated) {
    $message = 'Some of your Flag Lists where not updated due to incorrect Related Flags. Check your Flag Lists for errors and update them accordingly.';
  }
  else {
    $message = 'Updated the Flagging Collections Related flags to follow the new naming convention.';
  }
  return $message;
}

/**
 * Remove orphaned Flag List Items due to removed users.
 */
function flag_lists_update_8005() {
  $flagListsService = \Drupal::service('flaglists');
  $database = \Drupal::database();

  // Delete all flag list items.
  $query = $database->select('flag_list_item_field_data', 'flifd');
  $query->leftJoin('users', 'u', 'flifd.user_id = u.uid');
  $query
    ->fields('flifd', ['id'])
    ->isNull('u.uid');
  $result = $query->execute();
  foreach ($result as $record) {
    $id = $record->id;
    $flagListItem = $flagListsService->getFlagListItems([$id]);
    $flagListItem[$id]->delete();
  }

  $message = 'Removed orphaned Flag List Items.';
  return $message;
}

/**
 * Remove orphaned Flagging Collections due to removed users.
 */
function flag_lists_update_8006() {
  $flagListsService = \Drupal::service('flaglists');
  $database = \Drupal::database();

  // Remove all flagging collections.
  $query = $database->select('flagging_collection_field_data', 'fcfd');
  $query->leftJoin('users', 'u', 'fcfd.user_id = u.uid');
  $query
    ->fields('fcfd', ['id'])
    ->isNull('u.uid');
  $result = $query->execute();
  foreach ($result as $record) {
    $id = $record->id;
    $flaggingCollection = $flagListsService
      ->getFlaggingCollectionById($id);
    $flaggingCollection->delete();
  }

  $message = 'Removed orphaned Flagging Collections.';
  return $message;
}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc