You are here

mongodb_block_ui.module in MongoDB 7

Controls the visual building mongodb_block_uis a page is constructed with.

File

mongodb_block_ui/mongodb_block_ui.module
View source
<?php

/**
 * @file
 * Controls the visual building mongodb_block_uis a page is constructed with.
 */

/**
 * Implements hook_help().
 */
function mongodb_block_ui_help($path, $arg) {
  switch ($path) {
    case 'admin/help#mongodb_block':
      $output = '';
      $output .= '<h3>' . t('About') . '</h3>';
      $output .= '<p>' . t('The Block module allows you to create boxes of content, which are rendered into an area, or region, of one or more pages of a website. The core Seven administration theme, for example, implements the regions "Content", "Help", "Dashboard main", and "Dashboard sidebar", and a mongodb_block_ui may appear in any one of these regions. The <a href="@blocks">Blocks administration page</a> provides a drag-and-drop interface for assigning a mongodb_block_ui to a region, and for controlling the order of mongodb_block_uis within regions. For more information, see the online handbook entry for <a href="@mongodb_block_ui">Block module</a>.', array(
        '@block' => 'http://drupal.org/handbook/modules/mongodb_block/',
        '@blocks' => url('admin/structure/mongodb_block'),
      )) . '</p>';
      $output .= '<h3>' . t('Uses') . '</h3>';
      $output .= '<dl>';
      $output .= '<dt>' . t('Positioning content') . '</dt>';
      $output .= '<dd>' . t('When working with mongodb_block_uis, remember that all themes do <em>not</em> implement the same regions, or display regions in the same way. Blocks are positioned on a per-theme basis. Users with the <em>Administer mongodb blocks</em> permission can disable mongodb_block_uis. Disabled mongodb_block_uis are listed on the <a href="@mongodb_block_uis">Blocks administration page</a>, but are not displayed in any region.', array(
        '@mongodb_block_ui' => 'http://drupal.org/handbook/modules/mongodb_block_ui/',
        '@mongodb_block_uis' => url('admin/structure/mongodb_block_ui'),
      )) . '</dd>';
      $output .= '<dt>' . t('Controlling visibility') . '</dt>';
      $output .= '<dd>' . t('Blocks can be configured to be visible only on certain pages, only to users of certain roles, or only on pages displaying certain <a href="@content-type">content types</a>. Administrators can also allow specific mongodb_block_uis to be enabled or disabled by users when they edit their <a href="@user">My account</a> page. Some dynamic mongodb_block_uis, such as those generated by modules, will be displayed only on certain pages.', array(
        '@content-type' => url('admin/structure/types'),
        '@user' => url('user'),
      )) . '</dd>';
      $output .= '<dt>' . t('Creating custom mongodb_block_uis') . '</dt>';
      $output .= '<dd>' . t('Users with the <em>Administer mongodb blocks</em> permission can <a href="@mongodb_block_ui-add">add custom mongodb_block_uis</a>, which are then listed on the <a href="@mongodb_block_uis">Blocks administration page</a>. Once created, custom mongodb_block_uis behave just like default and module-generated mongodb_block_uis.', array(
        '@mongodb_block_uis' => url('admin/structure/mongodb_block_ui'),
        '@mongodb_block_ui-add' => url('admin/structure/mongodb_block_ui/add'),
      )) . '</dd>';
      $output .= '</dl>';
      return $output;
    case 'admin/structure/mongodb_block/add':
      return '<p>' . t('Use this page to create a new custom mongodb_block_ui.') . '</p>';
  }
}

/**
 * Implements hook_theme().
 */
function mongodb_block_ui_theme() {
  return array(
    'mongodb_block_ui_admin_display_form' => array(
      'template' => 'mongodb-block-ui-admin-display-form',
      'file' => 'mongodb_block_ui.admin.inc',
      'render element' => 'form',
    ),
  );
}

/**
 * Implements hook_permission().
 */
function mongodb_block_ui_permission() {
  return array(
    'administer mongodb blocks' => array(
      'title' => t('administer mongodb blocks'),
    ),
  );
}

/**
 * Implements hook_menu().
 */
function mongodb_block_ui_menu() {
  $default_theme = variable_get('theme_default', 'garland');
  $items['admin/structure/mongodb_block'] = array(
    'title' => 'Blocks',
    'description' => 'Configure what blocks content appears in your site\'s sidebars and other regions.',
    'page callback' => 'mongodb_block_ui_admin_display',
    'page arguments' => array(
      $default_theme,
    ),
    'access arguments' => array(
      'administer mongodb blocks',
    ),
    'file' => 'mongodb_block_ui.admin.inc',
  );
  $items['admin/structure/mongodb_block/list'] = array(
    'title' => 'List',
    'type' => MENU_DEFAULT_LOCAL_TASK,
    'weight' => -10,
  );
  $items['admin/structure/mongodb_block/manage/%/%/%'] = array(
    'title' => 'Configure mongodb_block',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'mongodb_block_ui_admin_configure',
      4,
      5,
      6,
    ),
    'access arguments' => array(
      'administer mongodb blocks',
    ),
    'file' => 'mongodb_block_ui.admin.inc',
  );
  $items['admin/structure/mongodb_block/manage/%/%/%/configure'] = array(
    'title' => 'Configure mongodb_blocks',
    'type' => MENU_DEFAULT_LOCAL_TASK,
    'context' => MENU_CONTEXT_INLINE,
  );
  $items['admin/structure/mongodb_block/manage/%/%/%/delete'] = array(
    'title' => 'Delete block',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'mongodb_block_ui_custom_block_delete',
      4,
      5,
      6,
    ),
    'access arguments' => array(
      'administer mongodb blocks',
    ),
    'type' => MENU_LOCAL_TASK,
    'context' => MENU_CONTEXT_NONE,
    'file' => 'mongodb_block_ui.admin.inc',
  );
  foreach (list_themes() as $key => $theme) {
    $items['admin/structure/mongodb_block/list/' . $key] = array(
      'title' => check_plain($theme->info['name']),
      'page arguments' => array(
        $key,
      ),
      'type' => $key == $default_theme ? MENU_DEFAULT_LOCAL_TASK : MENU_LOCAL_TASK,
      'weight' => $key == $default_theme ? -10 : 0,
      'access callback' => '_mongodb_block_ui_themes_access',
      'access arguments' => array(
        $theme,
      ),
      'file' => 'mongodb_block_ui.admin.inc',
    );
    $items['admin/structure/mongodb_block/list/' . $key . '/add'] = array(
      'title' => 'Add block',
      'page callback' => 'drupal_get_form',
      'page arguments' => array(
        'mongodb_block_ui_add_block_form',
        $key,
      ),
      'access arguments' => array(
        'administer mongodb blocks',
      ),
      'type' => MENU_LOCAL_ACTION,
      'file' => 'mongodb_block_ui.admin.inc',
    );
    $items['admin/structure/mongodb_block/demo/' . $key] = array(
      'title' => check_plain($theme->info['name']),
      'page callback' => 'mongodb_block_ui_admin_demo',
      'page arguments' => array(
        $key,
      ),
      'type' => MENU_CALLBACK,
      'access callback' => '_mongodb_block_ui_themes_access',
      'access arguments' => array(
        $theme,
      ),
      'theme callback' => '_mongodb_block_ui_custom_theme',
      'theme arguments' => array(
        $key,
      ),
      'file' => 'mongodb_block_ui.admin.inc',
    );
  }
  return $items;
}

/**
 * Menu item access callback - only admin or enabled themes can be accessed.
 */
function _mongodb_block_ui_themes_access($theme) {
  return user_access('administer mongodb blocks') && drupal_theme_access($theme);
}

/**
 * Implements hook_block_info().
 *
 * This function loads custom blocks.
 */
function mongodb_block_ui_block_info() {
  $blocks = array();
  $collection = mongodb_collection('block_custom');
  $result = $collection
    ->find(array(), array(
    'info',
    '_id',
  ))
    ->sort(array(
    'info' => 1,
  ));
  $blocks = new stdClass();
  foreach ($result as $block) {
    $delta = $block['_id'];

    // Here comes the magic... by using an object we can have string array
    // keys later when we cast to array.
    $blocks->{$delta} = array(
      'info' => $block['info'],
      // Not worth caching.
      'cache' => DRUPAL_NO_CACHE,
      'status' => TRUE,
    );
  }

  // Here , the $delta becomes a string.
  return (array) $blocks;
}

/**
 * Implements hook_mongodb_block_info_alter().
 *
 * This function loads block customizations.
 */
function mongodb_block_ui_mongodb_block_info_alter(&$blocks, $theme) {
  foreach (mongodb_collection('block_customized', $theme)
    ->find() as $block) {
    $index = $block['_id']['module'] . '_' . $block['_id']['delta'];
    if (!isset($blocks[$index])) {
      continue;
    }
    unset($block['_id']);
    if (isset($block['region']) && $block['region'] != MONGODB_BLOCK_REGION_NONE) {
      $block['status'] = TRUE;
    }
    $blocks[$index] = $block + $blocks[$index];
  }
}

/**
 * Implements hook_block_view().
 *
 * Generates the administrator-defined block for display.
 */
function mongodb_block_ui_block_view($delta = 0, $edit = array()) {
  $block = mongodb_block_ui_load($delta);
  $data['subject'] = check_plain($block['title']);
  $data['content'] = check_markup($block['body']['value'], $block['body']['format'], '', TRUE);
  return $data;
}

/**
 * Loads a block object.
 *
 * @param string $delta
 *   Unique ID of the block.
 *
 * @return array|null
 *   A document for the block of null if none was found.
 *
 * @throws \MongoConnectionException
 * @throws \MongoException
 * @throws \MongoWriteConcernException
 */
function mongodb_block_ui_load($delta) {
  return mongodb_collection('block_custom')
    ->findOne(array(
    '_id' => (string) $delta,
  ));
}

/**
 * Define the custom block form.
 */
function mongodb_block_ui_custom_block_form($edit = array()) {
  $edit += array(
    'info' => '',
    'body' => array(
      'value' => '',
      'format' => filter_default_format(),
    ),
  );
  $form['info'] = array(
    '#type' => 'textfield',
    '#title' => t('Block description'),
    '#default_value' => $edit['info'],
    '#maxlength' => 64,
    '#description' => t('A brief description of your block. Used on the <a href="@overview">Blocks administration page</a>.', array(
      '@overview' => url('admin/structure/mongodb_block'),
    )),
    '#required' => TRUE,
    '#weight' => -19,
  );
  $form['body_field']['#weight'] = -17;
  $form['body_field']['body'] = array(
    '#type' => 'text_format',
    '#title' => t('Block body'),
    '#default_value' => $edit['body']['value'],
    '#format' => $edit['body']['format'],
    '#rows' => 15,
    '#description' => t('The content of the block as shown to the user.'),
    '#required' => TRUE,
    '#weight' => -17,
    '#access' => filter_access(filter_format_load($edit['body']['format'])),
  );
  return $form;
}

/**
 * Implements hook_menu_delete().
 */
function mongodb_block_ui_menu_delete($menu) {
  $collection = mongodb_collection('block_custom');
  $collection
    ->remove([
    'module' => 'menu',
    'delta' => $menu['menu_name'],
  ], mongodb_default_write_options());
}

/**
 * Implements hook_form_FORM_ID_alter().
 */
function mongodb_block_ui_form_system_performance_settings_alter(&$form, &$form_state) {
  $disabled = count(module_implements('node_grants'));
  $form['caching']['block_cache'] = array(
    '#type' => 'checkbox',
    '#title' => t('Cache blocks'),
    // phpcs:ignore
    '#default_value' => variable_get('block_cache', FALSE),
    '#disabled' => $disabled,
    '#description' => $disabled ? t('Block caching is inactive because you have enabled modules defining content access restrictions.') : NULL,
    '#weight' => -1,
  );
}