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

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Dave Reid’s picture

Status: Active » Needs review
FileSize
8.13 KB
Dave Reid’s picture

Issue tags: +Needs backport to D7

We could easily backport this to Drupal 7 and mark node_is_page() and taxonomy_term_is_page() as deprecated.

Status: Needs review » Needs work

The last submitted patch, 1067126-entity-is-page.patch, failed testing.

Dave Reid’s picture

Revised patch that falls back to checking menu_get_item() to fix two of the test failures. Still fails the breadcrumb test...

Dave Reid’s picture

Status: Needs work » Needs review
FileSize
8.25 KB

Gah, 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.

Dave Reid’s picture

Realized this will cause paths like 'node/11' to match if node 1 is being passed in.

bleen’s picture

The patch in #6 works well for me ... obvious win by generalizing the two functions.

RTBC from me if testbot turns green...

fago’s picture

Status: Needs review » Needs work
+    elseif (strpos(current_path(), $uri['path'] . '/') === 0) {
+      // The path node/1/view also should match node 1's URI.
+      return TRUE;
+    }

But 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.

Dave Reid’s picture

Yes, 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.

sun’s picture

sun’s picture

Issue tags: -entity API, -entity cleanup

Standardizing on "entity" tag, which will be renamed to "Entity system".

Damien Tournoud’s picture

Those functions needs to go away. This information needs to be provided to the templates by a proper variable.

sbandyopadhyay’s picture

Just 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.

fago’s picture

Title: Add an entity_is_page() function to replace node_is_page() and taxonomy_term_is_page() » Support rendering entities in page mode
Issue tags: -Needs backport to D7

Now, 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():

$page: (optional) If set will control if the entity is rendered: if TRUE the entity will be rendered without its title, so that it can be embeded in another context. If FALSE the entity will be displayed with its title in a mode suitable for lists. If unset, the page mode will be enabled if the current path is the URI of the entity, as returned by entity_uri(). This parameter is only supported for entities which controller is a EntityAPIControllerInterface.

fago’s picture

Issue summary: View changes

Updated issue summary.

Version: 8.0.x-dev » 8.1.x-dev

Drupal 8.0.6 was released on April 6 and is the final bugfix release for the Drupal 8.0.x series. Drupal 8.0.x will not receive any further development aside from security fixes. Drupal 8.1.0-rc1 is now available and sites should prepare to update to 8.1.0.

Bug reports should be targeted against the 8.1.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.2.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.1.x-dev » 8.2.x-dev

Drupal 8.1.9 was released on September 7 and is the final bugfix release for the Drupal 8.1.x series. Drupal 8.1.x will not receive any further development aside from security fixes. Drupal 8.2.0-rc1 is now available and sites should prepare to upgrade to 8.2.0.

Bug reports should be targeted against the 8.2.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.6 was released on February 1, 2017 and is the final full bugfix release for the Drupal 8.2.x series. Drupal 8.2.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.3.0 on April 5, 2017. (Drupal 8.3.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.3.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.6 was released on August 2, 2017 and is the final full bugfix release for the Drupal 8.3.x series. Drupal 8.3.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.4.0 on October 4, 2017. (Drupal 8.4.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.4.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.4 was released on January 3, 2018 and is the final full bugfix release for the Drupal 8.4.x series. Drupal 8.4.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.5.0 on March 7, 2018. (Drupal 8.5.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.5.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.6 was released on August 1, 2018 and is the final bugfix release for the Drupal 8.5.x series. Drupal 8.5.x will not receive any further development aside from security fixes. Sites should prepare to update to 8.6.0 on September 5, 2018. (Drupal 8.6.0-rc1 is available for testing.)

Bug reports should be targeted against the 8.6.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.6.x-dev » 8.8.x-dev

Drupal 8.6.x will not receive any further development aside from security fixes. Bug reports should be targeted against the 8.8.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.9.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.7 was released on June 3, 2020 and is the final full bugfix release for the Drupal 8.8.x series. Drupal 8.8.x will not receive any further development aside from security fixes. Sites should prepare to update to Drupal 8.9.0 or Drupal 9.0.0 for ongoing support.

Bug reports should be targeted against the 8.9.x-dev branch from now on, and new development or disruptive changes should be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.2.x-dev

Drupal 8 is end-of-life as of November 17, 2021. There will not be further changes made to Drupal 8. Bugfixes are now made to the 9.3.x and higher branches only. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.15 was released on June 1st, 2022 and is the final full bugfix release for the Drupal 9.3.x series. Drupal 9.3.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.4.x-dev branch from now on, and new development or disruptive changes should be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.9 was released on December 7, 2022 and is the final full bugfix release for the Drupal 9.4.x series. Drupal 9.4.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.5.x-dev branch from now on, and new development or disruptive changes should be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

smustgrave’s picture

Assigned: Dave Reid » Unassigned
Issue summary: View changes

Checking 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.

Version: 9.5.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.