Comments

charginghawk created an issue. See original summary.

charginghawk’s picture

Status: Active » Needs review
charginghawk’s picture

Version: 8.x-1.x-dev » 8.x-1.3
StatusFileSize
new3.07 KB

The above doesn't actually work in D8, nor does it apply to 8.x-1.3. Here's a patch does both.

bobbysaul’s picture

I believe the default code should be 301 since that is what it is on the UI form page. Also, code 0 will throw an error.

bobbysaul’s picture

StatusFileSize
new3.08 KB

The patch in #3 works except the drush log fails because the URL object needs its toString() function to be called to print. Attached is the updated patch, along with the changes to the default code from my above comment. It should be noted that this patch is for drush 8.

bobbysaul’s picture

StatusFileSize
new3.81 KB

I missed the toString call in the check for loops.
I also made the command available for drush 9.

charginghawk’s picture

Status: Needs review » Reviewed & tested by the community

When I created this issue, I used a migration job before I got a chance to try it out. Ran into this use case again, tried the patch out and it works gangbusters. Thanks @bobbysaul!

bobbysaul’s picture

StatusFileSize
new7.25 KB

I am re-uploading my patch since the new files were not added with my last upload because I forgot git diff does not include untracked files.

This patch should include the new files for drush 9 support for this feature.

berdir’s picture

Status: Reviewed & tested by the community » Needs work
  1. +++ b/composer.json
    @@ -1,10 +1,18 @@
    +    "minimum-stability": "dev",
    +    "require": {
    +        "drupal/core": "~8",
    +        "php": ">=5.6.0"
    +    },
    

    is the php requirement really necessary? with 8.8, drupal core requires PHP 7 anyway.

  2. +++ b/composer.json
    @@ -1,10 +1,18 @@
    +                "drush.services.yml": "^9"
    

    afaik it makes sense to specify ^9 || ^10 now.

  3. +++ b/redirect.drush.inc
    @@ -19,10 +23,61 @@ function redirect_drush_command() {
    +      'code' => "Redirect code, one of 30{0-7}. Defaults to 301.",
    

    Per #2920454: Setting 300 Code as Default Redirect causes Fatal error (300 not supported by symfony http-foundation), this should be 301-307.

  4. +++ b/src/Commands/RedirectCommands.php
    @@ -0,0 +1,83 @@
    +/**
    + * A Drush commandfile.
    + *
    + * In addition to this file, you need a drush.services.yml
    + * in root of your module, and a composer.json file that provides the name
    + * of the services file to use.
    

    Maybe just "Redirect Drush commands". The current documentation seems copied from an example.

  5. +++ b/src/Commands/RedirectCommands.php
    @@ -0,0 +1,83 @@
    +class RedirectCommands extends DrushCommands {
    +  /**
    +   * The redirect repository.
    

    nitpick: empty line above /**

  6. +++ b/src/Commands/RedirectCommands.php
    @@ -0,0 +1,83 @@
    +      return drush_set_error('redirect', dt('You are attempting to redirect the page to itself. This would result in an infinite loop.'));
    ...
    +      return drush_set_error('redirect', dt('The source path %source is already being redirected.', [
    +        '%source' => $redirect->getSourceUrl(),
    ...
    +    drush_log(dt('Redirect from !source to !destination saved.', ['!source' => $redirect->getSourceUrl(), '!destination' => $redirect->getRedirectUrl()->toString()]), 'success');
    

    I think there are replacements available for these functions in D9+, $this->logger() and $this->io()->error() I believe.

lolcode’s picture

StatusFileSize
new2.73 KB

The patch was no longer applying for me on Drupal 8.8 and redirect 1.6.

I have updated it and covered almost everything in #9 except the use of $this->io() which gave me an error. $this->logger() was fine.

Other changes:
It looked to me like there was some confusion over the name between the class and the hook_drush_commands?
One was using "redirect-create" and the other was "create-redirect".
I used the value from the class. If the other way is preferred I would be happy to change.

I called the class in the redirect.drush.inc so that the code was all in one place.

lolcode’s picture

Status: Needs work » Needs review
StatusFileSize
new9.42 KB

Please ignore #10. I forgot to add the new files to the git diff. The patch did not contain the class.

lolcode’s picture

StatusFileSize
new6.4 KB

Sorry, please ignore #11. This time without extra git diff output that caused the fail.

berdir’s picture

diff --git a/composer.json b/composer.json
index bae3099..6a4ed14 100644

index bae3099..6a4ed14 100644
--- a/composer.json

--- a/composer.json
+++ b/composer.json

+++ b/composer.json
@@ -1,9 +1,16 @@

@@ -1,9 +1,16 @@
 {
-  "name": "drupal/redirect",
-  "description": "Allows users to redirect from old URLs to new URLs.",

@@ -0,0 +1,92 @@
+  public function redirectCreate($source, $destination, array $options = ['language' => Language::LANGCODE_NOT_SPECIFIED, 'code' => 301]) {
+    // Create redirect object and set initial values.
+    /** @var \Drupal\redirect\Entity\Redirect $redirect */
+    $redirect = Redirect::create();
+    $redirect->setSource($source);
+    $redirect->setRedirect($destination);
+    $options = $options + ['language' => Language::LANGCODE_NOT_SPECIFIED, 'code' => 301];
+    if ($options['language']) {

seems unnecessary to specify the defaults in the constructor when we need to initialize it in the function again anyway?

lolcode’s picture

What is the issue with the composer.json changes?
I allowed my IDE to update the indenting from 4 to 2. I checked that 4 spaces is correct for Drupal projects.
Looking at the patch it looks like it has the intended effect?

berdir’s picture

Status: Needs review » Needs work

Ignore that, I just ad that accidentally still selected, the comment is just about the constructor. Not very fond of making unrelated changes like that in patches, but not so important.

lolcode’s picture

Status: Needs work » Needs review
StatusFileSize
new6.28 KB
new981 bytes

I have updated the constructor to remove the default spec.
I also removed two unused imports now that the hook uses the class.

fjgarlin’s picture

Status: Needs review » Needs work

When adding a redirect that is already there.

Error: Call to undefined function Drupal\redirect\Commands\drush_set_error() in /var/www/html/web/modules/contrib/redirect/src/Commands/RedirectCommands.php

Tested with drush version : 10.6.2

ushma’s picture

StatusFileSize
new6.25 KB

Drush 10 does not support drush_set_error() function. I have replaced it with throw new \Exception()

ushma’s picture

Status: Needs work » Needs review
fjgarlin’s picture

StatusFileSize
new5.88 KB

Re-roll patch as it didn't apply anymore.

Oussama OUDRHIRI’s picture

StatusFileSize
new6.45 KB
new1.69 KB

Using this patch "drush_create_redirect-3040413-20.patch"

this command : drush redirect-create '/test' 'test-redirected' --code=301
=>returns this error message : "The "--code" option does not accept a value."
the same command with no "--code" option: drush redirect-create '/test/1' 'test-redirected/1'
=>set's the status_code to "NULL" in the database and when editing from the Back Office the changes are not persisted

Issue: the options are not taken into consideration.
Fix => I have updated the constructor to add the default spec.

Note: also fixed some SonarLint warnings

joey-santiago’s picture

The patch #21 seems to work fine for me. Also the --code parameter seems to work fine.

joey-santiago’s picture

Status: Needs review » Reviewed & tested by the community
Oussama OUDRHIRI’s picture

Version: 8.x-1.3 » 8.x-1.x-dev
Status: Reviewed & tested by the community » Needs review
StatusFileSize
new11.48 KB
new8.32 KB

When creating redirects in large quantities, we frequently encounter situations where certain source paths are already being redirected. In Patch #21, an exception is raised specifically for this scenario. However, when the redirect is successfully saved, no information is displayed in the terminal.

In Patch #24:

  • Replaced the usage of "$this->logger()->info()" with "$this->logger()->success()" for terminal output.
  • Introduced an update option with the "--update" flag for the "redirect-create" command, allowing the modification of the destination and code for an existing redirect.
  • Implemented a new command "redirect-update" exclusively designed for updating redirects, with filtering based on source path and language.
berdir’s picture

Status: Needs review » Needs work

This needs to be a merge request now and .drush.inc shouldn't be touched, no need to add that anymore.

malcomio made their first commit to this issue’s fork.

malcomio’s picture

Status: Needs work » Needs review