Question

For my Page content type, I want nodes created by administrators to be in my Simple XML Sitemap, but I want to exclude nodes created by non-admin users.

Administrators have permission to administer Simple XML Sitemap, and regular users do not.

On the Page content type settings, I have checked the box to index nodes of type Page by default. (You must enable indexing by default in order to get the Simple XML Sitemap settings widget on the node edit screen.)

I tried to change the settings in hook_form_alter(), but it doesn't work because non-admin users do not ever see the Simple Sitemap widget (it is not attached to the form if the user does not have admin permission for Simple XML Sitemap).

So I'm stuck:

  • I have to enable indexing by default on the node edit page to get the widget to appear.
  • Non-admin users do not have access to the widget, so I cannot change the value for non-admin users with hook_form_alter() (I have to accept the default).

How do I resolve this paradox?

Was it helpful?

Solution

I found the answer based on this drupal.org comment.

You have to use hook_node_insert().

/**
 * Implements hook_node_insert() for MYMODULE.
 */
function MYMODULE_node_insert(EntityInterface $node) {
  switch ($node->getType()) {
    case 'page':
      $user = Drupal::currentUser();
      if (!$user->hasPermission('administer content')) {
        // Turn off Simple XML sitemap.
        $generator = \Drupal::service('simple_sitemap.generator');
        $generator->setEntityInstanceSettings('node', $node->id(), ['index' => FALSE]);
      }
      break;
  }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with drupal.stackexchange
scroll top