Remove nojs from menu callbacks in favour of content negotiation from the request object

Created on 4 November 2012, over 12 years ago
Updated 19 March 2025, 21 days ago

Over in #1667742-19: Add abstracted dialog to core (resolves accessibility bug) β†’ the patch at #19 demonstrates that you can use the Request object and Content negotiation from the DIC to do away with nojs in your menu callbacks, ie decide whether the user has js based on the content negotiation.
This is a task to remove this logic from views.
Discussed with tim.plunkett on irc.

Example negotiation

// Check for the content-type.
$container = drupal_container();
$type = FALSE;
if ($container->has('request') && $container->has('content_negotiation')) {
  $type = $container->get('content_negotiation')->getContentType($container->get('request'));
}
if ($type == 'ajax') {
  // ...
}

I did try to sneak a utility wrapper for this into #1737148: Explicitly declare all JS dependencies, don't use drupal_add_js β†’ but it was removed until a case could be made for it being re-usable.
Perhaps this is that case?

/**
 * Utility method to get the normalized type of the current request.
 *
 * Fetches the ContentNegotiation and Request objects from the current container
 * and returns the associated request content type. The normalized type is a
 * short, lowercase version of the format, such as 'html', 'json' or 'atom'.
 *
 * @return string
 *   The request type
 */
function drupal_get_request_content_type() {
  $type = &drupal_static(__FUNCTION__);

  if (isset($type)) {
    return $type;
  }
  else {
    $container = drupal_container();
    if ($container->has('request') &&
        $container->has('content_negotiation')) {
      $type = $container->get('content_negotiation')->getContentType($container->get('request'));
    }
    else {
      $type = FALSE;
    }
  }
  return $type;
}
πŸ“Œ Task
Status

Postponed: needs info

Version

11.0 πŸ”₯

Component

ajax system

Created by

πŸ‡¦πŸ‡ΊAustralia larowlan πŸ‡¦πŸ‡ΊπŸ.au GMT+10

Live updates comments and jobs are added and updated live.
  • VDC

    Related to the Views in Drupal Core initiative.

  • stale-issue-cleanup

    To track issues in the developing policy for closing stale issues, [Policy, no patch] closing older issues

Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • πŸ‡ΊπŸ‡ΈUnited States smustgrave

    Thank you for creating this issue to improve Drupal.

    We are working to decide if this task is still relevant to a currently supported version of Drupal. There hasn't been any discussion here for over 8 years which suggests that this has either been implemented or is no longer relevant. Your thoughts on this will allow a decision to be made.

    Since we need more information to move forward with this issue, the status is now Postponed (maintainer needs more info). If we don't receive additional information to help with the issue, it may be closed after three months.

    Thanks!

Production build 0.71.5 2024