View source
<?php
require_once 'bricks.edit.inc';
function bricks_ctools_plugin_directory($module, $plugin) {
if ($module == 'entityreference') {
return 'plugins/' . $plugin;
}
}
function bricks_module_implements_alter(&$implementations, $hook) {
if ($hook == 'field_widget_form_alter') {
$group = $implementations['bricks'];
unset($implementations['bricks']);
$implementations['bricks'] = $group;
}
}
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');
}
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);
$hooks = array(
'entity',
);
foreach ($hooks as $hook) {
if (!isset($theme_registry[$hook]['theme paths'])) {
$theme_registry[$hook]['theme paths'] = array();
}
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);
}
}
}
}
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']);
}
}
}
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'];
}
}
}
}
function _bricks_tree_items($element, $items) {
$items = array_intersect_key($items, element_children($items));
foreach ($items as $delta => $item) {
$item = reset($item);
$item = reset($item);
$item['bricks'] = array();
$items[$delta] = $item;
}
$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];
}
$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;
}
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']);
}
}
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);
}
}
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'] : '',
],
);
}
function bricks_process_references_dialog_links(&$vars) {
$vars['links'][] = array(
'title' => t('Remove'),
'href' => '#',
'attributes' => array(
'class' => 'er-remove',
),
);
}
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'];
}