Describe your bug or feature request.

We do not want users clicking through these contextual links to the direct profile forms. The profiles should only be editable here in the context of the relevant modals.

Issue fork commerce-3559859

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

tbkot created an issue. See original summary.

jsacksick’s picture

@tbkot: I'm actually concerned this might not play well with caching... As the rendered entity should be cached in cache_render bin right? And adding a "cache" route context might essentially make the cache useless right? Also I'm afraid we're messing with other entity types right?

rszrama’s picture

Status: Active » Needs review

Seems to do the trick for me. Will let jsacksick decide if this is the best approach vs. a more targeted alteration.

jsacksick’s picture

I went with a hook_preprocess_HOOK() instead for profiles. It seems to be invoked every time, but I think it should potentially suffer from the same theoretical issue no? (i.e not invoked if the rendered entity is in cache)?

What's interesting is the Layout builder module does the following:

/**
 * Implements hook_entity_build_defaults_alter().
 */
function layout_builder_entity_build_defaults_alter(array &$build, EntityInterface $entity, $view_mode) {
  // Contextual links are removed for entities viewed in Layout Builder's UI.
  // The route.name.is_layout_builder_ui cache context accounts for this
  // difference.
  // @see layout_builder_entity_view_alter()
  // @see \Drupal\layout_builder\Cache\LayoutBuilderUiCacheContext
  $build['#cache']['contexts'][] = 'route.name.is_layout_builder_ui';

I thought my logic was invoked all the time due to this, but even after uninstalling the Layout Builder module the preprocess hook is still invoked... I'm wondering what is the best course of action... Add a "route" cache context from a hook_ENTITY_TYPE_build_defaults_alter() and keep the alteration where it is or move it to a hook_ENTITY_TYPE_view_alter()

Also the logic removes the contextual links on any route having an order parameter. This for example covers the modal route when opened outside of the modal.

jsacksick’s picture

I did this instead, to be on the safe side...
If Layout Builder does something similar for ALL entity types, I guess we can "safely" do the same.

/**
 * Implements hook_ENTITY_TYPE_build_defaults_alter().
 */
function commerce_order_profile_build_defaults_alter(array &$build, EntityInterface $entity, $view_mode): void {
  // Contextual links are removed for profiles viewed from order routes.
  $build['#cache']['contexts'][] = 'route';
}

/**
 * Implements hook_ENTITY_TYPE_view_alter().
 */
function commerce_order_profile_view_alter(array &$build, EntityInterface $entity, EntityViewDisplayInterface $display): void {
  // Remove contextual links for profiles on order routes.
  if (\Drupal::routeMatch()->getParameter('commerce_order')) {
    unset($build['#contextual_links']);
  }
}
rszrama’s picture

Status: Needs review » Reviewed & tested by the community

Worked for me on local!

  • jsacksick committed 7b098638 on 3.x authored by tbkot
    feat: #3559859 Disable contextual links on billing / shipping...
jsacksick’s picture

Status: Reviewed & tested by the community » Fixed

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

Status: Fixed » Closed (fixed)

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

davidwhthomas’s picture

FWIW, I have a site that isn't using the modal approach and needed to preserve the contextual links for profiles on orders.

This alter hook provides an override option to keep contextual links on order routes.

/**
 * Implements hook_module_implements_alter().
 *
 * Removes commerce_order's profile_view_alter implementation so that
 * contextual links are preserved on order routes.
 */
function MODULE_module_implements_alter(&$implementations, $hook): void {
  if ($hook === 'profile_view_alter' && isset($implementations['commerce_order'])) {
    unset($implementations['commerce_order']);
  }
}

Just adding in case anyone else is looking to preserve contextual profile links on orders.