View source
<?php
function bricks_init() {
drupal_add_js(drupal_get_path('module', 'bricks') . '/bricks.js');
}
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, $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);
$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, $module_path);
}
else {
array_unshift($theme_registry[$hook]['theme paths'], $module_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 ($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']);
}
}
}
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;
}
$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;
}
function bricks_process_field(&$vars) {
$element =& $vars['element'];
if ($element['#field_name'] == 'field_body') {
$vars['items'] = _bricks_tree_items($element, $vars['items']);
}
}
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'] : '',
),
);
}
}
function bricks_process_references_dialog_links(&$vars) {
$vars['links'][] = array(
'title' => t('Remove'),
'href' => '#',
'attributes' => array(
'class' => 'er-remove',
),
);
}