You are here

bricks.module in Bricks​ 7.4

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

File

bricks.module
View source
<?php

/**
 * Implements hook_init().
 */
function bricks_init() {
  drupal_add_js(drupal_get_path('module', 'bricks') . '/bricks.js');
}

/**
 * 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() as described here: https://www.drupal.org/node/715160.
 */
function bricks_theme_registry_alter(&$theme_registry, $module = 'bricks') {
  $theme_registry_copy = $theme_registry;
  $module_path = drupal_get_path('module', $module);
  _theme_process_registry($theme_registry_copy, 'phptemplate', 'theme_engine', $module, $module_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, $module_path);
      }
      else {
        array_unshift($theme_registry[$hook]['theme paths'], $module_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 ($element['#bundle'] == 'body' && 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;
  }

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

/**
 * Processes variables for theme_field().
 */
function bricks_process_field(&$vars) {
  $element =& $vars['element'];
  if ($element['#field_name'] == 'field_body') {
    $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 ($field['type'] == 'entityreference' && $field['field_name'] == 'field_body') {
    $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;
    $element['options'] = array(
      '#tree' => 'true',
      '#weight' => 11,
      'classes' => array(
        '#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',
    ),
  );
}