You are here

url_redirect.install in Url Redirect 8.2

Same filename and directory in other branches
  1. 8 url_redirect.install
  2. 7 url_redirect.install

File

url_redirect.install
View source
<?php

/**
 * Set the user value to be anonymous user if the field was left empty.
 */
function url_redirect_update_8101() {
  $redirects = \Drupal::entityTypeManager()
    ->getStorage('url_redirect')
    ->getQuery()
    ->notExists('user')
    ->execute();
  foreach ($redirects as $redirect) {
    $entity = \Drupal::entityTypeManager()
      ->getStorage('url_redirect')
      ->load($redirect);
    $entity
      ->set('user', [
      [
        'target_id' => '0',
      ],
    ]);
    $entity
      ->save();
  }
}

/**
 * Add negate key to url_redirect entity.
 */
function url_redirect_update_8102() {
  $manager = \Drupal::entityDefinitionUpdateManager();
  $entity_type = $manager
    ->getEntityType('url_redirect');
  $entity_keys = $entity_type
    ->getKeys();
  $entity_keys['negate'] = 'negate';
  $entity_type
    ->set('entity_keys', $entity_keys);
  $manager
    ->updateEntityType($entity_type);
}

/**
 * Migrate old redirect data.
 */
function url_redirect_update_8103() {
  $db_connection = \Drupal::database();
  if ($db_connection
    ->schema()
    ->tableExists('url_redirect')) {
    $query = $db_connection
      ->select('url_redirect', 'ur');
    $query
      ->fields('ur');
    $all_data = $query
      ->execute()
      ->fetchAll();
    foreach ($all_data as $key => $data) {
      $values = array(
        'label' => 'Migrated Redirect',
        'id' => 'migrated_' . $key . '_' . time(),
        'path' => $data->path,
        'redirect_path' => $data->redirect_path,
        'roles' => Drupal\Component\Serialization\Json::decode($data->roles),
        'users' => Drupal\Component\Serialization\Json::decode($data->users),
        'status' => $data->status,
        'message' => $data->message,
        'checked_for' => $data->check_for,
      );
      $url_redirect_entity = \Drupal\url_redirect\Entity\UrlRedirect::create($values);
      $url_redirect_entity
        ->save();
    }

    // We can drop 'url_redirect' table once we confirm everything is migrated properly.
    // $db_connection->schema()->dropTable('url_redirect');
  }
}

Functions

Namesort descending Description
url_redirect_update_8101 Set the user value to be anonymous user if the field was left empty.
url_redirect_update_8102 Add negate key to url_redirect entity.
url_redirect_update_8103 Migrate old redirect data.