Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
We're duplicating code that could be generalized into one simple function:
node_is_page($node)
taxonomy_term_is_page($term)
Could just be the following:
/**
* Returns whether the current page is the page of the passed in entity.
*
* @param $type
* The entity type; e.g. 'node' or 'user'.
* @param $entity
* The entity object.
*
* @return
* TRUE if the current page is the page of the specified entity, or FALSE
* otherwise.
*/
function entity_is_page($type, $entity) {
return (($uri = entity_uri($type, $entity)) && current_path() == $uri['path']);
}
Related issues
#721754: A node cannot be displayed in different view mode on its own page
Comment | File | Size | Author |
---|---|---|---|
#6 | 1067126-entity-is-page.patch | 8.38 KB | Dave Reid |
#5 | 1067126-entity-is-page.patch | 8.25 KB | Dave Reid |
#4 | 1067126-entity-is-page.patch | 8.41 KB | Dave Reid |
#1 | 1067126-entity-is-page.patch | 8.13 KB | Dave Reid |
Comments
Comment #1
Dave ReidComment #2
Dave ReidWe could easily backport this to Drupal 7 and mark node_is_page() and taxonomy_term_is_page() as deprecated.
Comment #4
Dave ReidRevised patch that falls back to checking menu_get_item() to fix two of the test failures. Still fails the breadcrumb test...
Comment #5
Dave ReidGah, so we're better off checking strpos(current_path(), $uri['path']) since that replicates the same behavior as menu_get_object(). This should pass the tests.
Comment #6
Dave ReidRealized this will cause paths like 'node/11' to match if node 1 is being passed in.
Comment #7
bleen CreditAttribution: bleen commentedThe patch in #6 works well for me ... obvious win by generalizing the two functions.
RTBC from me if testbot turns green...
Comment #8
fagoBut that would match for node/1/edit too?
Instead of determining the context when it was already lost, I think we should pass the context top-down, e.g. add this to an option to a future API function like entity_view().
Also, I don't like the function name. The entity is no page - entity_view_is_page() would fit better imho.
Comment #9
Dave ReidYes, the current node_is_page( ) also matches 'node/1/edit' as well. This patch is merely joining together the functions into one function and then we can improve from there.
Comment #10
sunSee also #721754: A node cannot be displayed in different view mode on its own page
Comment #11
sunStandardizing on "entity" tag, which will be renamed to "Entity system".
Comment #12
Damien Tournoud CreditAttribution: Damien Tournoud commentedThose functions needs to go away. This information needs to be provided to the templates by a proper variable.
Comment #13
sbandyopadhyay CreditAttribution: sbandyopadhyay commentedJust as entity_uri() allows for a bundle-specific callback (
$info['bundles'][$bundle]['uri callback']
) to take precedence over the generic one for the entity type ($info['uri callback']
), it would be great for entity_is_page() to likewise allow bundle-specific callbacks.Comment #14
fagoNow, as we have an entity render API I think we should introduce the $page parameter there. Checking for the uri is not sufficient also, as we might have multiple pages for an entity - we won't be able to backport this though.
entity API module has that in d7 at entity_view():
Comment #14.0
fagoUpdated issue summary.
Comment #27
smustgrave CreditAttribution: smustgrave at Mobomo commentedChecking the state of D10 this may still be a valid feature request if anyone picks it up. Will need an issue summary update I imagine though.