Problem/Motivation

Currently, the Drupal IndexNow module does not provide a way for users to dynamically alter the IndexNow submission URL and the key location URL. For greater flexibility and customization, especially for advanced use cases, it would be beneficial to introduce a mechanism (like hooks or settings) that allows users to modify these URLs to meet their specific needs.

Proposed resolution

Drupal's hook system, such as hook_indexnow_alter()

Issue fork index_now-3557839

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

rajghai created an issue. See original summary.

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

macsim’s picture

Title: Allow Users to Alter IndexNow URL and Key Location URL » Allow Users to alter Key Location URL
Status: Active » Needs review

While altering the key location URL could be a good idea, IMHO there's no reason to alter the page_url sent to index_now since it is an absolute URL of the entity.

There was an issue with projects using domains module with a domain per language that has been fixed on 3.1.x-dev #3558253: Error after submitting translation of node

I updated the issue title in that sense + removed the code you wrote about hook_index_now_page_url_alter() and only kept the hook_index_now_key_location_url_alter() + updated the tests

Can you please apply the patch https://git.drupalcode.org/project/index_now/-/merge_requests/34.diff and tell me if it suits your needs ?

macsim’s picture

Status: Needs review » Postponed (maintainer needs more info)

Postpone this issue because I am not sure it is still needed.
I guess you had an issue because you were using domains module + index now and the bug has been fixed in #3558253: Error after submitting translation of node

I would need more information about your use case

macsim’s picture

Title: Allow Users to alter Key Location URL » Allow Users to alter URLs submitted to Indew Now and Key Location URL
Assigned: Unassigned » macsim
Status: Postponed (maintainer needs more info) » Needs work

The headless CMS use case described in #3586683: Provide a way to set a custom base URL for the index (headless CMS) is actually a compelling reason to provide a hook for altering the URLs submitted to Index Now - reopening this issue accordingly.

macsim’s picture

Title: Allow Users to alter URLs submitted to Indew Now and Key Location URL » Allow Users to alter URLs submitted to Index Now and Key Location URL

macsim’s picture

Assigned: macsim » Unassigned
Status: Needs work » Needs review
  • !34 is for 3.1.x
  • !60 is for 4.0.x

Manual testing procedure

The unit tests cover the hook invocation logic, but the following smoke test
verifies the full integration in a real Drupal instance.

Prerequisites

  • A Drupal site with Index Now installed and configured (valid API key, a
    search engine selected)
  • Verbose mode enabled at /admin/config/services/index_now so
    submissions appear in the logs

Step 1 — Implement the hook in a custom module

Create a file my_test.module in a temporary custom module and
add the following:

<?php
use Drupal\node\NodeInterface;

function my_test_index_now_url_alter(string &$url, array $context): void {
  $entity = $context['entity'] ?? NULL;
  // Log what we receive to verify the hook fires and the entity is available.
  \Drupal::logger('my_test')->info(
    'hook_index_now_url_alter fired. Original URL: @url. Entity: @entity.',
    [
      '@url' => $url,
      '@entity' => $entity instanceof NodeInterface
        ? $entity->getEntityTypeId() . ':' . $entity->id()
        : 'none',
    ]
  );
  // Optionally alter the URL to verify it is the one actually submitted:
  // $url = 'https://example.com/custom-path';
}

Step 2 — Trigger a ping

Save any existing node. Index Now should submit its URL to the configured
search engine.

Step 3 — Check the logs

Go to /admin/reports/dblog and filter by type
my_test. Verify that:

  • The log entry appears (hook was invoked)
  • The Original URL matches the node's canonical URL
  • The Entity field shows node:&lt;id&gt; (entity is
    correctly passed in context)

Also check the index_now log entries to confirm the URL
submitted to the search engine matches what the hook returned.

Step 4 — Test hook_index_now_key_location_url_alter
(optional)

Add a second implementation to the same module:

function my_test_index_now_key_location_url_alter(string &$key_location): void {
  \Drupal::logger('my_test')->info('keyLocation: @loc', ['@loc' => $key_location]);
}

Save a node again and verify the log entry shows the expected key location
URL (should match $settings['index_now.base_url'] if configured,
or the Drupal site URL otherwise).

macsim’s picture

Status: Needs review » Reviewed & tested by the community

  • macsim committed bbbb0329 on 3.1.x
    Resolve #3557839 "Allow users to alter Key Location URL"
    

  • macsim committed 6c849d01 on 4.0.x
    Issue #3557839 by rajghai, macsim: Allow Users to alter URLs submitted...
macsim’s picture

Status: Reviewed & tested by the community » Fixed

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.