Commit e61ef3d0 authored by Thalles Ferreira's avatar Thalles Ferreira Committed by Sascha Grossenbacher
Browse files

Issue #2961293 by deepakkumar14, RoshniPatel.addweb, thalles, dww: Convert...

Issue #2961293 by deepakkumar14, RoshniPatel.addweb, thalles, dww: Convert module to use short array syntax (new coding standard)
parent 02ced4ac
Loading
Loading
Loading
Loading
+7 −7
Original line number Diff line number Diff line
@@ -9,16 +9,16 @@
 * Implements hook_drush_command().
 */
function redirect_drush_command() {
  $items['generate-redirects'] = array(
  $items['generate-redirects'] = [
    'description' => 'Create redirects.',
    'drupal dependencies' => array('devel_generate'),
    'arguments' => array(
    'drupal dependencies' => ['devel_generate'],
    'arguments' => [
      'count' => 'Number of redirects to generate.',
    ),
    'options' => array(
    ],
    'options' => [
      'delete' => 'Delete all redirects before generating new ones.',
    ),
  );
    ],
  ];

  return $items;
}
+24 −24
Original line number Diff line number Diff line
@@ -15,21 +15,21 @@ use Drupal\redirect\Entity\Redirect;
 */

function redirect_generate_form() {
  $form['count'] = array(
  $form['count'] = [
    '#type' => 'textfield',
    '#title' => t('How many URL redirects would you like to generate?'),
    '#default_value' => 50,
    '#size' => 4,
  );
  $form['delete'] = array(
  ];
  $form['delete'] = [
    '#type' => 'checkbox',
    '#title' => t('Delete all URL redirects before generating new URL redirects.'),
    '#default_value' => FALSE,
  );
  $form['submit'] = array(
  ];
  $form['submit'] = [
    '#type' => 'submit',
    '#value' => t('Generate'),
  );
  ];

  return $form;
}
@@ -42,28 +42,28 @@ function redirect_generate_form_submit(&$form, &$form_state) {

function redirect_generate_redirects_batch_info($count, $delete = FALSE) {
  if ($delete) {
    $operations[] = array('redirect_generate_batch_delete', array());
    $operations[] = ['redirect_generate_batch_delete', []];
  }

  $operations[] = array('redirect_generate_batch_generate', array($count));
  $operations[] = ['redirect_generate_batch_generate', [$count]];

  return array(
  return [
    'operations' => $operations,
    'finished' => 'redirect_generate_batch_finished',
    'file' => drupal_get_path('module', 'redirect') . '/redirect.generate.inc',
  );
  ];
}

function redirect_generate_batch_delete(array &$context) {
  if (empty($context['sandbox'])) {
    $context['sandbox'] = array();
    $context['sandbox'] = [];
    $context['sandbox']['progress'] = 0;
    $context['sandbox']['current_rid'] = 0;
    $context['sandbox']['max'] = db_query('SELECT COUNT(DISTINCT rid) FROM {redirect}')->fetchField();
  }

  $limit = 20;
  $rids = db_query_range("SELECT rid FROM {redirect} WHERE rid > :rid ORDER BY rid", 0, $limit, array(':rid' => $context['sandbox']['current_rid']))->fetchCol();
  $rids = db_query_range("SELECT rid FROM {redirect} WHERE rid > :rid ORDER BY rid", 0, $limit, [':rid' => $context['sandbox']['current_rid']])->fetchCol();
  foreach (redirect_repository()->loadMultiple($rids) as $redirect) {
    $redirect->delete();
  }
@@ -71,7 +71,7 @@ function redirect_generate_batch_delete(array &$context) {
  // Update our progress information.
  $context['sandbox']['progress'] += count($rids);
  $context['sandbox']['current_rid'] = end($rids);
  $context['message'] = t('Deleted URL redirect @rid.', array('@rid' => end($rids)));
  $context['message'] = t('Deleted URL redirect @rid.', ['@rid' => end($rids)]);

  // Inform the batch engine that we are not finished,
  // and provide an estimation of the completion level we reached.
@@ -82,7 +82,7 @@ function redirect_generate_batch_delete(array &$context) {

function redirect_generate_batch_generate($num, array &$context) {
  if (empty($context['sandbox'])) {
    $context['sandbox'] = array();
    $context['sandbox'] = [];
    $context['sandbox']['progress'] = 0;
    $context['sandbox']['max'] = $num;
    $query = \Drupal::database()->select('node', 'n');
@@ -96,7 +96,7 @@ function redirect_generate_batch_generate($num, array &$context) {

  $limit = 20;
  $types = array_keys(redirect_status_code_options());
  $languages = \Drupal::moduleHandler()->moduleExists('locale') ? array_keys(\Drupal::languageManager()->getLanguages()) : array();
  $languages = \Drupal::moduleHandler()->moduleExists('locale') ? array_keys(\Drupal::languageManager()->getLanguages()) : [];

  for ($i = 0; $i < min($limit, $context['sandbox']['max'] - $context['sandbox']['progress']); $i++) {
    $rand = mt_rand(0, 100);
@@ -104,8 +104,8 @@ function redirect_generate_batch_generate($num, array &$context) {
    $redirect = Redirect::create();

    $source = _redirect_generate_url();
    $source_options = array();
    $redirect_options = array();
    $source_options = [];
    $redirect_options = [];

    if ($context['sandbox']['nids'] && $rand >= 40) {
      $redirect_target = 'node/' . array_rand($context['sandbox']['nids']);
@@ -128,7 +128,7 @@ function redirect_generate_batch_generate($num, array &$context) {
      $redirect->setLanguage($languages[array_rand($languages)]);
    }

    $query = array();
    $query = [];
    if ($rand <= 30) {
      $query = _redirect_generate_querystring();
    }
@@ -141,10 +141,10 @@ function redirect_generate_batch_generate($num, array &$context) {
    if (mt_rand(0, 1)) {
    $query = \Drupal::database();
    $query->update('redirect')
      ->fields(array(
      ->fields([
        'count' => mt_rand(1, 500),
        'access' => mt_rand(REQUEST_TIME - 31536000, REQUEST_TIME),
      ))
      ])
      ->condition('rid', $redirect->id())
      ->execute();
    }
@@ -171,14 +171,14 @@ function redirect_generate_batch_finished($success, $results, $operations) {
    // An error occurred.
    // $operations contains the operations that remained unprocessed.
    $error_operation = reset($operations);
    drupal_set_message(t('An error occurred while processing @operation with arguments : @args', array('@operation' => $error_operation[0], '@args' => print_r($error_operation[0], TRUE))));
    drupal_set_message(t('An error occurred while processing @operation with arguments : @args', ['@operation' => $error_operation[0], '@args' => print_r($error_operation[0], TRUE)]));
  }
}

function _redirect_generate_url($external = FALSE, $max_levels = 2) {
  $url = array();
  $url = [];
  if ($external) {
    $tlds = array('com', 'net', 'org');
    $tlds = ['com', 'net', 'org'];
    $url[] = 'http://www.example.'. $tlds[array_rand($tlds)];
  }
  $max_levels = mt_rand($external ? 0 : 1, $max_levels);
@@ -189,6 +189,6 @@ function _redirect_generate_url($external = FALSE, $max_levels = 2) {
}

function _redirect_generate_querystring() {
  $query = array(DevelGenerateBase::generateWord(mt_rand(1, 3)) => DevelGenerateBase::generateWord(mt_rand(2, 4)));
  $query = [DevelGenerateBase::generateWord(mt_rand(1, 3)) => DevelGenerateBase::generateWord(mt_rand(2, 4))];
  return $query;
}
+19 −19
Original line number Diff line number Diff line
@@ -28,7 +28,7 @@ use Symfony\Component\Routing\Exception\RouteNotFoundException;
 * Implements hook_hook_info().
 */
function redirect_hook_info() {
  $hooks = array(
  $hooks = [
    'redirect_load',
    'redirect_load_by_source_alter',
    'redirect_access',
@@ -39,9 +39,9 @@ function redirect_hook_info() {
    'redirect_update',
    'redirect_delete',
    'redirect_alter',
  );
  ];

  return array_fill_keys($hooks, array('group' => 'redirect'));
  return array_fill_keys($hooks, ['group' => 'redirect']);
}

/**
@@ -106,7 +106,7 @@ function redirect_path_update(array $path) {
  // Delete all redirects having the same source as this alias.
  redirect_delete_by_path($path['alias'], $path['langcode'], FALSE);
  if ($original_path['alias'] != $path['alias']) {
    if (!redirect_repository()->findMatchingRedirect($original_path['alias'], array(), $original_path['langcode'])) {
    if (!redirect_repository()->findMatchingRedirect($original_path['alias'], [], $original_path['langcode'])) {
      $redirect = Redirect::create();
      $redirect->setSource($original_path['alias']);
      $redirect->setRedirect($path['source']);
@@ -162,23 +162,23 @@ function redirect_path_delete($path) {
function redirect_page_build(&$page) {
  if (redirect_is_current_page_404() && \Drupal::currentUser()->hasPermission('administer redirects')) {
    if (!isset($page['content']['system_main']['actions'])) {
      $page['content']['system_main']['actions'] = array(
      $page['content']['system_main']['actions'] = [
        '#theme' => 'links',
        '#links' => array(),
        '#attributes' => array('class' => array('action-links')),
        '#links' => [],
        '#attributes' => ['class' => ['action-links']],
        '#weight' => -100,
      );
      ];
    }
    // We cannot simply use current_path() because if a 404 path is set, then
    // that value overrides whatever is in $_GET['q']. The
    // drupal_deliver_html_page() function thankfully puts the original current
    // path into $_GET['destination'].
    $destination = drupal_get_destination();
    $page['content']['system_main']['actions']['#links']['add_redirect'] = array(
    $page['content']['system_main']['actions']['#links']['add_redirect'] = [
      'title' => t('Add URL redirect from this page to another location'),
      'href' => 'admin/config/search/redirect/add',
      'query' => array('source' => $destination['destination']) + drupal_get_destination(),
    );
      'query' => ['source' => $destination['destination']] + drupal_get_destination(),
    ];
  }
}

@@ -260,7 +260,7 @@ function redirect_sort_recursive(&$array, $callback = 'sort') {
/**
 * Build the URL of a redirect for display purposes only.
 */
function redirect_url($path, array $options = array(), $clean_url = NULL) {
function redirect_url($path, array $options = [], $clean_url = NULL) {
  // @todo - deal with removal of clean_url config. See
  //    https://drupal.org/node/1659580
  if (!isset($clean_url)) {
@@ -312,7 +312,7 @@ function redirect_url($path, array $options = array(), $clean_url = NULL) {
}

function redirect_status_code_options($code = NULL) {
  $codes = array(
  $codes = [
    300 => t('300 Multiple Choices'),
    301 => t('301 Moved Permanently'),
    302 => t('302 Found'),
@@ -320,7 +320,7 @@ function redirect_status_code_options($code = NULL) {
    304 => t('304 Not Modified'),
    305 => t('305 Use Proxy'),
    307 => t('307 Temporary Redirect'),
  );
  ];
  return isset($codes[$code]) ? $codes[$code] : $codes;
}

@@ -364,13 +364,13 @@ function _redirect_uasort($a, $b) {
 * Implements hook_form_FORM_ID_alter() on behalf of locale.module.
 */
function locale_form_redirect_edit_form_alter(array &$form, FormStateInterface $form_state) {
  $form['language'] = array(
  $form['language'] = [
    '#type' => 'select',
    '#title' => t('Language'),
    '#options' => array(Language::LANGCODE_NOT_SPECIFIED => t('All languages')) + \Drupal::languageManager()->getLanguages(),
    '#options' => [Language::LANGCODE_NOT_SPECIFIED => t('All languages')] + \Drupal::languageManager()->getLanguages(),
    '#default_value' => $form['language']['#value'],
    '#description' => t('A redirect set for a specific language will always be used when requesting this page in that language, and takes precedence over redirects set for <em>All languages</em>.'),
  );
  ];
}

/**
@@ -394,12 +394,12 @@ function redirect_get_redirect_operations() {
 * Implements hook_redirect_operations().
 */
function redirect_redirect_operations() {
  $operations['delete'] = array(
  $operations['delete'] = [
    'action' => t('Delete'),
    'action_past' => t('Deleted'),
    'callback' => 'redirect_delete_multiple',
    'confirm' => TRUE,
  );
  ];
  return $operations;
}

+18 −18
Original line number Diff line number Diff line
@@ -60,10 +60,10 @@ class Redirect extends ContentEntityBase {
   *   Base 64 hash.
   */
  public static function generateHash($source_path, array $source_query, $language) {
    $hash = array(
    $hash = [
      'source' => mb_strtolower($source_path),
      'language' => $language,
    );
    ];

    if (!empty($source_query)) {
      $hash['source_query'] = $source_query;
@@ -76,9 +76,9 @@ class Redirect extends ContentEntityBase {
   * {@inheritdoc}
   */
  public static function preCreate(EntityStorageInterface $storage_controller, array &$values) {
    $values += array(
    $values += [
      'type' => 'redirect',
    );
    ];
  }

  /**
@@ -148,7 +148,7 @@ class Redirect extends ContentEntityBase {
   * @param array $query
   *   Query arguments.
   */
  public function setSource($path, array $query = array()) {
  public function setSource($path, array $query = []) {
    $this->redirect_source->set(0, ['path' => ltrim($path, '/'), 'query' => $query]);
  }

@@ -204,7 +204,7 @@ class Redirect extends ContentEntityBase {
   * @param array $options
   *   The source url options.
   */
  public function setRedirect($url, array $query = array(), array $options = array()) {
  public function setRedirect($url, array $query = [], array $options = []) {
    $uri = $url . ($query ? '?' . UrlHelper::buildQuery($query) : '');
    $external = UrlHelper::isValid($url, TRUE);
    $uri = ($external ? $url : 'internal:/' . ltrim($uri, '/'));
@@ -284,42 +284,42 @@ class Redirect extends ContentEntityBase {
      ->setLabel(t('User ID'))
      ->setDescription(t('The user ID of the node author.'))
      ->setDefaultValueCallback('\Drupal\redirect\Entity\Redirect::getCurrentUserId')
      ->setSettings(array(
      ->setSettings([
        'target_type' => 'user',
      ));
      ]);

    $fields['redirect_source'] = BaseFieldDefinition::create('redirect_source')
      ->setLabel(t('From'))
      ->setDescription(t("Enter an internal Drupal path or path alias to redirect (e.g. %example1 or %example2). Fragment anchors (e.g. %anchor) are <strong>not</strong> allowed.", array('%example1' => 'node/123', '%example2' => 'taxonomy/term/123', '%anchor' => '#anchor')))
      ->setDescription(t("Enter an internal Drupal path or path alias to redirect (e.g. %example1 or %example2). Fragment anchors (e.g. %anchor) are <strong>not</strong> allowed.", ['%example1' => 'node/123', '%example2' => 'taxonomy/term/123', '%anchor' => '#anchor']))
      ->setRequired(TRUE)
      ->setTranslatable(FALSE)
      ->setDisplayOptions('form', array(
      ->setDisplayOptions('form', [
        'type' => 'redirect_link',
        'weight' => -5,
      ))
      ])
      ->setDisplayConfigurable('form', TRUE);

    $fields['redirect_redirect'] = BaseFieldDefinition::create('link')
      ->setLabel(t('To'))
      ->setRequired(TRUE)
      ->setTranslatable(FALSE)
      ->setSettings(array(
      ->setSettings([
        'link_type' => LinkItemInterface::LINK_GENERIC,
        'title' => DRUPAL_DISABLED
      ))
      ->setDisplayOptions('form', array(
      ])
      ->setDisplayOptions('form', [
        'type' => 'link',
        'weight' => -4,
      ))
      ])
      ->setDisplayConfigurable('form', TRUE);

    $fields['language'] = BaseFieldDefinition::create('language')
      ->setLabel(t('Language'))
      ->setDescription(t('The redirect language.'))
      ->setDisplayOptions('form', array(
      ->setDisplayOptions('form', [
        'type' => 'language_select',
        'weight' => 2,
      ));
      ]);

    $fields['status_code'] = BaseFieldDefinition::create('integer')
      ->setLabel(t('Status code'))
@@ -341,7 +341,7 @@ class Redirect extends ContentEntityBase {
   *   An array of default values.
   */
  public static function getCurrentUserId() {
    return array(\Drupal::currentUser()->id());
    return [\Drupal::currentUser()->id()];
  }

}
+1 −1
Original line number Diff line number Diff line
@@ -204,7 +204,7 @@ class RedirectRequestSubscriber implements EventSubscriberInterface {
    // This needs to run before RouterListener::onKernelRequest(), which has
    // a priority of 32. Otherwise, that aborts the request if no matching
    // route is found.
    $events[KernelEvents::REQUEST][] = array('onKernelRequestCheckRedirect', 33);
    $events[KernelEvents::REQUEST][] = ['onKernelRequestCheckRedirect', 33];
    return $events;
  }

Loading