You are here

bricks.module in Bricks​ 7.5

Same filename and directory in other branches
  1. 8 bricks.module
  2. 7.4 bricks.module
  3. 2.x bricks.module

File

bricks.module
View source
<?php

require_once 'bricks.edit.inc';

/**
 * Implements hook_ctools_plugin_directory().
 */
function bricks_ctools_plugin_directory($module, $plugin) {
  if ($module == 'entityreference') {
    return 'plugins/' . $plugin;
  }
}

/**
 * Implements hook_module_implements_alter().
 */
function bricks_module_implements_alter(&$implementations, $hook) {
  if ($hook == 'field_widget_form_alter') {
    $group = $implementations['bricks'];
    unset($implementations['bricks']);
    $implementations['bricks'] = $group;
  }
}

/**
 * Implements hook_theme_registry_alter().
 */
function bricks_theme_registry_alter(&$theme_registry) {
  _bricks_theme_registry_serve_path($theme_registry, 'bricks', drupal_get_path('module', 'bricks'));
  _bricks_theme_registry_serve_path($theme_registry, 'bricks', 'sites/all/templates');
}

/**
 * Helper for hook_theme_registry_alter() as described here: https://www.drupal.org/node/715160.
 */
function _bricks_theme_registry_serve_path(&$theme_registry, $theme, $path) {
  $theme_registry_copy = $theme_registry;
  _theme_process_registry($theme_registry_copy, 'phptemplate', 'theme_engine', $theme, $path);
  $theme_registry += array_diff_key($theme_registry_copy, $theme_registry);

  // A list of templates the module will provide templates for
  $hooks = array(
    'entity',
  );
  foreach ($hooks as $hook) {

    // Add the key 'theme paths' if it doesn't exist in this theme's registry
    if (!isset($theme_registry[$hook]['theme paths'])) {
      $theme_registry[$hook]['theme paths'] = array();
    }

    // Shift this module's directory to the top of the theme path list
    if (is_array($theme_registry[$hook]['theme paths'])) {
      $first_element = array_shift($theme_registry[$hook]['theme paths']);
      if ($first_element) {
        array_unshift($theme_registry[$hook]['theme paths'], $first_element, $path);
      }
      else {
        array_unshift($theme_registry[$hook]['theme paths'], $path);
      }
    }
  }
}

/**
 * Preprocesses variables for entity.tpl.php.
 */
function bricks_preprocess_entity(&$vars) {
  $element =& $vars['elements'];
  if (!empty($element['#options']['classes'])) {
    $vars['classes_array'][] = check_plain($element['#options']['classes']);
  }
  if ($element['#entity_type'] == 'brick') {
    $vars['title'] = '';
    if (!empty($vars['content']['field_title']['#items'][0]['value'])) {
      $vars['title'] = $vars['content']['field_title']['#items'][0]['value'];
      unset($vars['content']['field_title']);
    }
    $base_heading_level = isset($element['#base_heading_level']) ? $element['#base_heading_level'] : 1;
    $element['#heading_level'] = $base_heading_level + !empty($vars['title']);
    if (isset($vars['content']['field_body'])) {
      $vars['content']['bricks'] = _bricks_tree_items($element['field_body'], $vars['content']['field_body']);
      unset($vars['content']['field_body']);
    }
  }
}

/**
 * Processes variables for entity.tpl.php.
 */
function bricks_process_entity(&$vars) {
  $element =& $vars['elements'];
  if ($element['#entity_type'] == 'brick') {
    $vars['prefix'] = '<div class="' . $vars['classes'] . '"' . $vars['attributes'] . '>' . drupal_render($vars['title_prefix']) . drupal_render($vars['title_suffix']);
    $vars['suffix'] = '</div>';
    $vars['heading'] = !empty($vars['title']) ? '<h' . $element['#heading_level'] . '>' . $vars['title'] . '</h' . $element['#heading_level'] . '>' : '';
    $vars['placeholder'] = '<div class="er-placeholder">' . $element['#bundle'] . '</div>';
    if (isset($vars['content']['bricks'])) {
      foreach ($vars['content']['bricks'] as &$brick) {
        $brick['#base_heading_level'] = $element['#heading_level'];
      }
    }
  }
}

/**
 * Helper function: converts element's items to tree structure.
 */
function _bricks_tree_items($element, $items) {

  // Filter items:
  $items = array_intersect_key($items, element_children($items));

  // Simplify items:
  foreach ($items as $delta => $item) {
    $item = reset($item);
    $item = reset($item);
    $item['bricks'] = array();
    $items[$delta] = $item;
  }

  // Prepare items:
  $parents = array(
    -1,
  );
  $prev_depth = 0;
  foreach ($items as $delta => $item) {
    if (isset($element['#items'][$delta]['options'])) {
      $items[$delta]['#options'] = $element['#items'][$delta]['options'];
    }
    $depth = $element['#items'][$delta]['depth'];
    if ($depth > $prev_depth) {
      array_unshift($parents, $delta - 1);
    }
    else {
      if ($depth < $prev_depth) {
        array_splice($parents, 0, $prev_depth - $depth);
      }
    }
    $prev_depth = $depth;
    $items[$delta]['#parent_delta'] = $parents[0];
  }

  // Process items in reverse order (without recursion):
  $ritems = array_reverse($items, TRUE);
  foreach ($ritems as $delta => $item) {
    if ($item['#parent_delta'] != -1) {
      array_unshift($items[$item['#parent_delta']]['bricks'], $items[$delta]);
      unset($items[$delta]);
    }
  }
  return $items;
}

/**
 * Processes variables for theme_field().
 */
function bricks_process_field(&$vars) {
  $element =& $vars['element'];
  $field = field_info_field($element['#field_name']);
  if (_bricks_field_is_treeable($field)) {
    $vars['items'] = _bricks_tree_items($element, $vars['items']);
  }
}

/**
 * Implements hook_field_widget_form_alter().
 */
function bricks_field_widget_form_alter(&$element, &$form_state, $context) {
  $field = $context['field'];
  if (_bricks_field_is_treeable($field) && $context['instance']['widget']['type'] != 'inline_entity_form') {
    $element['target_id']['#size'] = 10;
    if (isset($element['entityreference_live_preview'])) {
      $element['entityreference_live_preview']['#weight'] = -1;
    }
    $item = isset($context['items'][$context['delta']]) ? $context['items'][$context['delta']] : NULL;
    _bricks_field_widget_elements($element, $item);
  }
}

/**
 * Adds bricks' related fields into entityreference field widget.
 *
 * @param array $element Render element of the field widget.
 * @param array $item Array of field property values to use as default values.
 */
function _bricks_field_widget_elements(&$element, $item) {
  $element['depth'] = array(
    '#type' => 'textfield',
    '#default_value' => isset($item['depth']) ? $item['depth'] : 0,
    '#size' => 1,
  );
  $element['options'] = array(
    '#tree' => 'true',
    '#weight' => 11,
    'classes' => [
      '#type' => 'textfield',
      '#attributes' => array(
        'placeholder' => t('Additional CSS classes'),
      ),
      '#default_value' => !empty($item['options']['classes']) ? $item['options']['classes'] : '',
    ],
  );
}

/**
 * Processes variables for theme_references_dialog_links().
 */
function bricks_process_references_dialog_links(&$vars) {
  $vars['links'][] = array(
    'title' => t('Remove'),
    'href' => '#',
    'attributes' => array(
      'class' => 'er-remove',
    ),
  );
}

/**
 * Helper function to check if entityreference field has enabled treeable behavior.
 *
 * @param object $field
 * @return bool
 */
function _bricks_field_is_treeable($field) {
  return $field['type'] == 'entityreference' && ($field_behaviors = $field['settings']['handler_settings']['behaviors']) && array_key_exists('treeable_behavior', $field_behaviors) && $field_behaviors['treeable_behavior']['status'];
}

Functions

Namesort descending Description
bricks_ctools_plugin_directory Implements hook_ctools_plugin_directory().
bricks_field_widget_form_alter Implements hook_field_widget_form_alter().
bricks_module_implements_alter Implements hook_module_implements_alter().
bricks_preprocess_entity Preprocesses variables for entity.tpl.php.
bricks_process_entity Processes variables for entity.tpl.php.
bricks_process_field Processes variables for theme_field().
bricks_process_references_dialog_links Processes variables for theme_references_dialog_links().
bricks_theme_registry_alter Implements hook_theme_registry_alter().
_bricks_field_is_treeable Helper function to check if entityreference field has enabled treeable behavior.
_bricks_field_widget_elements Adds bricks' related fields into entityreference field widget.
_bricks_theme_registry_serve_path Helper for hook_theme_registry_alter() as described here: https://www.drupal.org/node/715160.
_bricks_tree_items Helper function: converts element's items to tree structure.