While porting https://www.drupal.org/project/entity_is_public to Drupal 8 I found that I need a way to test the Rabbit Hole action for different scenarios.

\Drupal\rabbit_hole\BehaviorInvoker::processEntity() returns either a response object or NULL. I tried working my way out of that but I realized that I could do way more specific tests by making \Drupal\rabbit_hole\BehaviorInvoker::getRabbitHoleValuesForEntity() public and checking for each specific action than inspecting response objects.

CommentFileSizeAuthor
#11 2958548-11.patch1.59 KBAnybody
#2 rabbit_hole-2958548-2.patch608 bytesjuampynr
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

juampynr created an issue. See original summary.

juampynr’s picture

Status: Active » Needs review
FileSize
608 bytes

Here is a patch that changes the access modifier to public.

deviantintegral’s picture

Does it make more sense for this method to always return a response, modified or not? That would save callers from having to check for null returns. I think it's fine as a different issue and patch, I was just looking based on your issue description.

Otherwise, this patch looks good to me.

juampynr’s picture

There is a higher level method that calls this one that returns a response or NULL. I feel that it was overhead to go through that instead of exposing this method.

juampynr’s picture

Actually, now that I have got the tests passing, I will try inspecting the response and see if I get the same results. That would mean that this patch is not required.

juampynr’s picture

Fixed at https://github.com/juampynr/entity_is_public/commit/15677b60cdcad8754d79.... The patch is no longer necessary. However, I will leave this open as it may be useful to have that method available as part of the Rabit Hole API.

firewaller’s picture

When I use the code from GitHub in a custom module I get the following error:

Fatal error: Call to a member function getStatusCode() on null

use Symfony\Component\HttpFoundation\Response;

/**
 * Implements hook_entity_update().
 */
function MY_MODULE_entity_update(EntityInterface $entity) {
  /** @var \Drupal\node\NodeInterface; $entity */
  if ($entity instanceof NodeInterface) {
    if (\Drupal::moduleHandler()->moduleExists('rabbit_hole')) {
      /** @var \Drupal\rabbit_hole\BehaviorInvoker $rabbit_hole_behavior_invoker */
      $rabbit_hole_behavior_invoker = \Drupal::service('rabbit_hole.behavior_invoker');
      /** @var \Symfony\Component\HttpFoundation\Response $response */
      $response = $rabbit_hole_behavior_invoker->processEntity($entity);

      \Drupal::logger('status_code')->notice($response->getStatusCode());
    }
  }
}

Any ideas?

firewaller’s picture

I missed the NULL check:

$response_unchanged = NULL;
if ($response == $response_unchanged) {
  return TRUE;
}
else {
  return ($response->getStatusCode() >= 200) && ($response->getStatusCode() < 400);
}

I updated my code to:

...

$response_unchanged = NULL;
if ($response != $response_unchanged) {
  \Drupal::logger('status_code')->notice($response->getStatusCode());
}

Nevermind. Thank you!

Anybody’s picture

Same here! :)
I've been struggling around with this problem for several days now for rabbit_hole_href module and rh is too strict and too less separated in this case (while it's a wonderful module!). Other modules extending rh need better ways to figure out which entities are affected by rh and how. Drupal is built on extensions and I think rh is now that common that it should also allow other modules to extend it for special use cases as best as possible.

Of course you "could" write workarounds like in #6 based on processEntity() but to analyze the returned response is very dirty and the module could use some API love here :)

In my concrete example what I needed was, given just an entity type + bundle name OR an entity object, information about its configured rh behavior.

What rh currently misses as public functions / services is:

  • Get the rabbit_hole configuration for an entity_type / bundle (if bundled)
  • Get the rabbit_hole configuration for an entity (with fallback to parent bundle / entity type) like written above in patch #2

I'll write a patch as soon as possible, it would be nice to have some rh maintainer feedback in the meantime?

Thank you all!

Anybody’s picture

Related issue for better access to rabbit_hole information from other modules: #3095653: Split performAction() into submethods in PageRedirect Behaviour

Anybody’s picture

FileSize
1.59 KB

Patch attached based on #2 which also adds public function getRabbitHoleValuesForEntityType($entity_type_id, $bundle_id = NULL) to retrieve the configuration for an entity type + bundle in cases where no entity object exists.

Please review and feedback.

Anybody’s picture

Could a maintainer perhaps have a look at the patch in #11? We're using it since that date in production and it works perfectly. Would be nice to have this reviewed. Thank you very much.

thomas.frobieter’s picture

Status: Needs review » Reviewed & tested by the community

We're using patch #11 successfully since months now. +1 RTBC

Dylan Donkersgoed’s picture

Status: Reviewed & tested by the community » Fixed

Only real problem I see with this is that the method probably should be in the interface as well, but the already existing public method doesn't do that either so it's probably not really in scope for this issue. Merged this in. Thanks Anybody and juampynr for the patch and thomas.frobieter for confirming it's functioning long term on your site.

Anybody’s picture

Great, thank you Dylan! One patch less! YAY! :)

Looking forward to the next stable release.

Status: Fixed » Closed (fixed)

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