You are here

function quickedit_page_build in Quick Edit 7

Implements hook_page_build().

Adds the quickedit library to the page for any user who has the 'access in-place editing' permission.

File

./quickedit.module, line 125
Provides in-place content editing functionality for fields.

Code

function quickedit_page_build(&$page) {

  // If the user lacks the appropriate permission, return early to avoid
  // processing.
  if (!user_access('access in-place editing')) {
    return;
  }

  // In-place editing is only supported on the front-end.
  if (path_is_admin(current_path())) {
    return;
  }
  elseif (!empty($page['content']['system_main']['#node_edit_form'])) {
    return;
  }

  // Incredibly ugly hack to support Panelizer overrides using Page Manager.
  // @see includes/panelizer.inc/quickedit_ctools_render_alter()
  global $quickedit_workaround_for_fundamentally_broken_page_manager;
  if ($quickedit_workaround_for_fundamentally_broken_page_manager) {

    // Change from
    //   <div class="contextual-links-wrapper">
    // to
    //   <div class="contextual-links-wrapper" data-quickedit-is-contextual-region-for-entity>
    $page['content']['system_main']['#attributes']['data-quickedit-is-contextual-region-for-entity'] = '';
  }

  // Abuse the 'page_top' region for attaching our libraries.
  $page['page_top']['#attached']['library'][] = array(
    'quickedit',
    'quickedit',
  );

  // Certain themes don't add region wrappers, so we can't assume region
  // wrappers are present. Therefore, Quick Edit must inject its own
  // alternative: start and end markers for the "content" region.
  $page['content']['#theme_wrappers'][] = 'quickedit_wrap_content_region';

  // Provide the user ID and permissions hash in Drupal.settings to allow
  // JavaScript code to maintain client-side caches.
  // @see Drupal 8's user_page_build()
  // @see Drupal 8's \Drupal\user\PermissionsHash
  // @see https://drupal.org/node/2005644
  global $user;
  $roles = array_keys($user->roles);
  $serialized_roles = implode(',', $roles);
  if ($cache = cache_get("quickedit_user_permissions_hash:{$serialized_roles}", 'cache_page')) {
    $permissions_hash = $cache->data;
  }
  else {
    $permissions_hash = hash('sha256', drupal_get_hash_salt() . serialize(user_role_permissions($user->roles)));

    // Use the 'cache_page' bin along with CACHE_TEMPORARY, because the submit
    // callback for the form responsible for changing user permissions calls
    // cache_clear_all() without arguments, which clears the page cache, and
    // hence it will also clear this cache entry.
    cache_set("quickedit_user_permissions_hash:{$serialized_roles}", $permissions_hash, 'cache_page', CACHE_TEMPORARY);
  }
  $page['page_top']['#attached']['js'][] = array(
    'type' => 'setting',
    'data' => array(
      'quickedit' => array(
        'user' => array(
          'uid' => $user->uid,
          'permissionsHash' => $permissions_hash,
        ),
      ),
    ),
  );
}