Problem/Motivation
I have configured a content type (Article) to automatically be included on the sitemap.
However, there are a few nodes that I want to exclude from the sitemap programmatically (because I am importing nodes using the Feeds module).
Based on this post, I tried the following code in hook_node_presave():
$generator = \Drupal::service('simple_sitemap.generator');
$generator->entityManager()->setEntityInstanceSettings('node', $node->id(), ['index' => false]);
However, when I open the node edit page, the node is still set to be indexed. What more do I need to do to exclude the node from the sitemap?
Comments
Comment #2
gbyteSo what I understand is you are importing nodes programmatically (you are not using the node creation form) and would like to exclude said nodes from the sitemap permanently during the import.
You are on the right track, but I would change three things:
If you want to exclude the node in a particular variant only, do e.g
$manager->setVariants(['default'])instead.Comment #3
ptmkenny commentedThanks, that code worked!
I agree that hook_entity_insert() is generally better, but in my case the nodes might get re-imported (and the values updated), so in case the sitemap exclusion value changes, I want to check it every time.
Comment #4
debasish147 commentedIn hook_entity_insert() , above mentioned service is not working, but it is working during hook_entity_update().
Comment #5
ptmkenny commented@Debasish147 hook_entity_insert() runs after the node has been saved, so it's too late to change the properties of the node.
You can use hook_entity_presave() and check node->isNew() to adjust the settings when the node is saved for the first time.
Comment #6
debasish147 commented@ptmkenny in hook_entity_presave(), inside node->isNew() condition, node id is not available as node is not save yet, so the above service is throwing error as it is required second parameter as node id.
Comment #7
ptmkenny commented@Debasish147 Oh right, I forgot about that. Sorry, I answered without checking my code :(
Yes, as you say, it doesn't work on the initial node save. But, I wanted to set this on the node insert. So, I did it like this.
Set it in hook_node_presave() while checking for $node->isNew() to avoid a crash.
And then, in hook_node_insert:
Forcefully resaving the node is not elegant but it does work.
Comment #8
debasish147 commented@ptmkenny Thanks for the code.
Comment #9
gbyte#3034070: Store entity instance settings as fields on entity should make the above easier once it's implemented.
Comment #11
ptmkenny commentedUpon more testing, one problem with running this in `hook_entity_presave()` is that it will only resolve successfully when saving the node programmatically.
When using this code:
If you save the node through the Drupal admin UI, the sitemap settings will not be changed, and whatever was set when the node was saved in the UI will be set.
Comment #12
gbyte@ptmkenny Yes, the only way to do it on entity form save is to add a form submit handler via hook_form_alter and make sure it runs after simple_sitemap's form submit handler.
Comment #13
pshetty9005 commentedDid anyone get this working for nodes created through the UI?
Comment #14
phjouGo the same issue as #11, I had to use a form alter to achieve it.
In my case I had a field_enable_redirect and wanted to disable the sitemap when it was set to true, but you can change to use whatever custom behavior you need.
Comment #15
julienvey commentedThe issue described in #11 is due to the module simple_sitemap overwritting the variant with the value of the form_state. Basically the node gets excluded in your hook_entity_presave() and then gets included again in the EntityFormHandler.php of the module.
#14 does the trick, but it means your node will be excluded two times : one in the EntityFormHandler.php and one in your hook_entity_presave().
One solution is to unset the value :)
The function of @phjou then becomes :