View source
<?php
include_once 'oa_related.features.inc';
include_once 'oa_related.paragraphs.inc';
include_once 'oa_related.paragraphs.info.inc';
include_once 'oa_related.theme.inc';
define('PARAGRAPH_ADD_MORE_BUNDLE', 'add_more_bundle_');
function oa_related_ctools_plugin_directory($owner, $plugin_type) {
if ($owner == 'ctools' && $plugin_type == 'content_types') {
return 'plugins/content_types';
}
}
function oa_related_theme($existing, $type, $theme, $path) {
$theme = array();
$path = $path . '/templates';
$theme['oa_related_files'] = array(
'template' => 'oa-related-files',
'variables' => array(),
'path' => $path,
);
$theme['oa_related_ref'] = array(
'template' => 'oa-related-ref',
'variables' => array(),
'path' => $path,
);
$theme['oa_related_paragraphs_media_render'] = array(
'path' => $path,
'base hook' => 'entity',
'template' => 'oa-related-paragraphs-media-render',
'render element' => 'element',
);
$theme['oa_related_paragraphs_text_render'] = array(
'path' => $path,
'base hook' => 'entity',
'template' => 'oa-related-paragraphs-text-render',
'render element' => 'element',
);
$theme['oa_related_paragraphs_content_render'] = array(
'path' => $path,
'base hook' => 'entity',
'template' => 'oa-related-paragraphs-content-render',
'render element' => 'element',
);
$theme['oa_related_paragraphs_snippet_render'] = array(
'path' => $path,
'base hook' => 'entity',
'template' => 'oa-related-paragraphs-snippet-render',
'render element' => 'element',
);
$bundles = paragraphs_bundle_load();
foreach ($bundles as $machine_name => $bundle) {
$filename = 'oa-related-paragraphs-' . $machine_name . '-form';
if (file_exists($path . '/' . $filename . '.tpl.php')) {
$theme['oa_related_paragraphs_' . $machine_name . '_form'] = array(
'template' => $filename,
'render element' => 'element',
'path' => $path,
);
}
}
return $theme;
}
function oa_related_panels_display_save($display) {
if (!empty($display->cache_key)) {
$parts = explode(':', $display->cache_key);
if (!empty($parts[2]) && $parts[2] == 'node' && !empty($parts[3])) {
list($bundle, $viewmode) = explode('.', $parts[3]);
if ($viewmode == 'featured') {
_oa_related_enable_related($bundle, _oa_related_has_panelizer_viewmode($bundle, $viewmode));
}
}
}
}
function oa_related_node_type_update($info) {
_oa_related_enable_related($info->type, _oa_related_has_panelizer_viewmode($info->type, 'featured'));
}
function _oa_related_has_panelizer_viewmode($type, $viewmode) {
$handler = panelizer_entity_plugin_get_handler('node');
return !empty($handler->plugin['bundles'][$type]['status']) && !empty($handler->plugin['bundles'][$type]['view modes'][$viewmode]['status']);
}
function _oa_related_enable_related($type, $value = TRUE) {
$field = field_info_field('field_oa_related_content');
if (!$field) {
return;
}
if (!og_is_group_content_type('node', $type)) {
return;
}
if ($value) {
$do_update = empty($field['settings']['handler_settings']['target_bundles'][$type]);
$field['settings']['handler_settings']['target_bundles'][$type] = $type;
}
else {
$do_update = !empty($field['settings']['handler_settings']['target_bundles'][$type]);
unset($field['settings']['handler_settings']['target_bundles'][$type]);
}
if ($do_update) {
field_update_field($field);
$message = $value ? t('Enabled !type for related content references.', array(
'!type' => $type,
)) : t('Disabled !type for related content references.', array(
'!type' => $type,
));
if (variable_get('install_task') == 'done') {
drupal_set_message($message);
}
}
}
function oa_related_form_field_ui_display_overview_form_alter(&$form, &$form_state) {
if (isset($form['#entity_type']) && $form['#entity_type'] == 'paragraphs_item') {
foreach ($form['#fields'] as $fieldname) {
if (isset($form['fields'][$fieldname]['label'])) {
if (isset($form['fields'][$fieldname]['label']['#options'])) {
$form['fields'][$fieldname]['label']['#options']['heading'] = t('Heading');
}
}
}
}
}
function oa_related_paragraphs_field_widget_form_alter(&$element, &$form_state, &$context) {
$element['#theme'] = 'oa_related_paragraphs_' . $element['#bundle'] . '_form';
$bundle_title = trim(str_replace('Paragraph type:', '', $element['paragraph_bundle_title']['info']['#markup']));
$element['paragraph_bundle_title']['info']['#markup'] = $bundle_title;
$element['actions']['remove_button']['#value'] = t('Remove !title', array(
'!title' => rtrim(strip_tags($bundle_title)),
));
}
function oa_related_default_pane($node, $field_name) {
$fields = field_info_instances('node', $node->type);
$instance = $fields[$field_name];
$field_display = $instance['display']['default'];
$pane = panels_new_pane('entity_field', 'node:' . $field_name, TRUE);
$pane->configuration['formatter'] = $field_display['type'];
$pane->configuration['formatter_settings'] = $field_display['settings'];
$pane->configuration['label'] = $field_display['label'];
$pane->configuration['context'] = 'panelizer';
return $pane;
}
function oa_related_panelizer_pre_render_alter(&$panelizer, $display, $entity) {
if (!empty($entity->field_oa_related)) {
$found = FALSE;
foreach ($display->content as $index => $panel) {
if ($panel->type == 'entity_field' && $panel->subtype == 'node:field_oa_related') {
$found = TRUE;
break;
}
}
if (!$found) {
foreach ($display->content as $index => $panel) {
if ($panel->type == 'entity_field' && $panel->subtype == 'node:body') {
$pane = oa_related_default_pane($entity, 'field_oa_related');
$pid = uniqid();
$pane->pid = $pid;
$pane->uuid = $pid;
$pane->panel = $panel->panel;
$panelizer->display->content[$pid] = $pane;
$i = array_search($index, $panelizer->display->panels[$panel->panel]);
array_splice($panelizer->display->panels[$panel->panel], $i + 1, 0, array(
$pid,
));
break;
}
}
}
}
}
function oa_related_get_best_section($type, $options = array(), $space_id = NULL) {
return module_exists('oa_sections') ? oa_sections_get_best_section($type, $options, $space_id) : NULL;
}
function oa_related_form_node_form_alter(&$form, &$form_state) {
if (isset($form[OA_SECTION_FIELD])) {
if (!empty($_GET['render']) && $_GET['render'] == 'references-dialog') {
$node = $form['#node'];
$section = oa_related_get_best_section($node->type, $form[OA_SECTION_FIELD][LANGUAGE_NONE]['#options']);
if (!empty($section)) {
$form[OA_SECTION_FIELD][LANGUAGE_NONE]['#default_value'] = $section;
}
$section_field_weight = isset($form['body']['#weight']) ? $form['body']['#weight'] - 0.5 : 0;
$form[OA_SECTION_FIELD]['#weight'] = $section_field_weight;
if (isset($form['#group_children'])) {
unset($form['#group_children'][OA_SECTION_FIELD]);
}
}
}
if (isset($form['field_oa_related']) && isset($form['body'])) {
$form['field_oa_related']['#weight'] = $form['body']['#weight'] + 0.5;
}
}
function oa_related_features_template_info() {
$data =& drupal_static(__FUNCTION__);
if (empty($data)) {
$content_types = oa_related_get_content_types();
$data = array();
foreach ($content_types as $content_type => $present) {
if ($present || field_info_instance('node', 'field_oa_related', $content_type) && node_type_get_type($content_type)) {
$data[] = array(
'plugin' => 'field_instance',
'template' => 'oa_related_field_default_field_instances_template',
'deleted' => empty($present),
'entity_type' => 'node',
'bundle_type' => $content_type,
'field' => 'field_oa_related',
);
}
}
}
return $data;
}
function oa_related_get_content_types() {
$allowed_default = module_invoke_all('oa_related_allowed_default');
$allowed_default = drupal_map_assoc($allowed_default);
return array_intersect_key(variable_get('oa_related_allowed_types', $allowed_default), node_type_get_names());
}
function oa_related_field_default_field_instances_template() {
$field_instances = array();
foreach (paragraphs_bundle_load() as $machine_name => $bundle) {
$bundles[$machine_name] = $machine_name;
}
$field_instances['node-oa_wiki_page-field_oa_related'] = array(
'bundle' => 'oa_wiki_page',
'comment_alter' => 0,
'default_value' => NULL,
'deleted' => 0,
'description' => '',
'display' => array(
'default' => array(
'label' => 'hidden',
'module' => 'paragraphs',
'settings' => array(
'view_mode' => 'full',
),
'type' => 'paragraphs_view',
'weight' => 1,
),
'featured' => array(
'label' => 'above',
'settings' => array(),
'type' => 'hidden',
'weight' => 0,
),
'ical' => array(
'label' => 'above',
'settings' => array(),
'type' => 'hidden',
'weight' => 0,
),
'teaser' => array(
'label' => 'above',
'settings' => array(),
'type' => 'hidden',
'weight' => 0,
),
),
'entity_type' => 'node',
'field_name' => 'field_oa_related',
'label' => t('Paragraphs'),
'required' => 0,
'settings' => array(
'add_mode' => 'button',
'add_mode_instructions' => '',
'allowed_bundles' => array_intersect_key($bundles, array_filter(variable_get('oa_related_paragraph_bundles', $bundles))),
'default_edit_mode' => 'open',
'hide_instructions' => 1,
'title' => 'Paragraph',
'title_multiple' => 'Paragraphs',
'user_register_form' => FALSE,
),
'widget' => array(
'active' => 0,
'module' => 'paragraphs',
'settings' => array(),
'type' => 'paragraphs_embed',
'weight' => 4,
),
);
return $field_instances;
}
function oa_related_oa_settings_form(&$form_state) {
$forms = array();
$form = array();
$allowed_default = module_invoke_all('oa_related_allowed_default');
drupal_map_assoc($allowed_default);
$types = oa_core_list_content_types(TRUE, TRUE);
$options = array();
foreach ($types as $key => $type) {
$options[$key] = $type->name;
}
$default = variable_get('oa_related_allowed_types', $allowed_default);
$form['oa_related_allowed_types'] = array(
'#type' => 'checkboxes',
'#title' => 'Content that can reference other content',
'#description' => 'Enable which content types should have a Related Content field.',
'#options' => $options,
'#default_value' => $default,
);
$form_state['build_info']['oa_related_allowed_types'] = $default;
$bundles = array();
$_bundles = paragraphs_bundle_load();
foreach ($_bundles as $machine_name => $bundle) {
$bundles[$machine_name] = $bundle->name;
}
$form['oa_related_paragraph_bundles'] = array(
'#title' => t('Enabled paragraph bundles'),
'#description' => t('Select which bundles related content field should have.'),
'#type' => 'checkboxes',
'#required' => TRUE,
'#default_value' => array_filter(variable_get('oa_related_paragraph_bundles', array_keys($bundles))),
'#options' => $bundles,
);
$form_state['oa_related_paragraph_bundles'] = $form['oa_related_paragraph_bundles']['#default_value'];
$forms[] = array(
'caption' => t('Related Content'),
'form' => $form,
'submit' => 'oa_related_settings_submit',
);
return $forms;
}
function oa_related_settings_submit($form, &$form_state) {
$update = FALSE;
if (!empty($form_state['build_info']['oa_related_allowed_types']) && md5(serialize($form_state['build_info']['oa_related_allowed_types'])) != md5(serialize($form_state['values']['oa_related_allowed_types']))) {
variable_set('oa_related_allowed_types', $form_state['values']['oa_related_allowed_types']);
$update = TRUE;
}
if (isset($form_state['values']['oa_related_paragraph_bundles'])) {
$filtered = array_filter($form_state['values']['oa_related_paragraph_bundles']);
variable_set('oa_related_paragraph_bundles', $filtered);
$form_state['values']['oa_related_paragraph_bundles'] = $filtered;
$update = $update || $form_state['oa_related_paragraph_bundles'] != $filtered;
}
if ($update) {
features_template_revert();
}
}
function oa_related_preprocess_entity(&$vars) {
if ($vars['entity_type'] == 'paragraphs_item') {
$field_name = NULL;
$info = oa_related_get_paragraph_field_info();
$value = NULL;
$paragraph_wrapper = entity_metadata_wrapper('paragraphs_item', $vars['paragraphs_item']);
$vars['oa_related_content'] = '';
foreach ($info as $field_name => $bundle) {
foreach ($bundle as $bundle_name => $data) {
if ($paragraph_wrapper->bundle
->value() == $bundle_name) {
if (isset($paragraph_wrapper->{$field_name})) {
$value = $paragraph_wrapper->{$field_name}
->value();
}
if (isset($data['render callback'])) {
if (is_array($data['render callback'])) {
foreach ($data['render callback'] as $function) {
if (function_exists($function)) {
$function($vars, $bundle_name, $field_name, $value);
drupal_alter($function, $vars, $bundle_name, $field_name, $value);
}
}
}
else {
$function = $data['render callback'];
if (function_exists($function)) {
$function($vars, $bundle_name, $field_name, $value);
drupal_alter($function, $vars, $bundle_name, $field_name, $value);
}
}
}
}
}
}
}
}
function oa_related_field_attach_presave($entity_type, $entity) {
if ($entity_type == 'paragraphs_item') {
if (isset($entity->field_paragraph_lock_revision)) {
$lock = field_get_items('paragraphs_item', $entity, 'field_paragraph_lock_revision');
if (isset($lock)) {
foreach ($entity as $field_name => $field) {
if (is_array($field) && isset($field[LANGUAGE_NONE][0]['revision_id'])) {
if (!empty($lock[0]['value'])) {
$original_items = isset($entity->original) ? (array) field_get_items($entity_type, $entity->original, $field_name) : array();
$revision_id = !empty($field[LANGUAGE_NONE][0]['revision_id']) ? $field[LANGUAGE_NONE][0]['revision_id'] : NULL;
$revision_id = !empty($original_items[0]['revision_id']) ? $original_items[0]['revision_id'] : $revision_id;
if ($revision_id) {
$entity->{$field_name}[LANGUAGE_NONE][0]['revision_id'] = $revision_id;
}
}
else {
unset($entity->{$field_name}[LANGUAGE_NONE][0]['revision_id']);
}
}
}
}
}
}
}
function oa_related_default_search_api_index_alter(&$data) {
if (isset($data['database_node_index'])) {
$data['database_node_index']->options['fields']['field_oa_related'] = array(
'type' => 'list<integer>',
'entity_type' => 'paragraphs_item',
);
$data['database_node_index']->options['fields']['field_oa_related:field_paragraph_text:value'] = array(
'type' => 'list<text>',
);
$data['database_node_index']->options['fields']['field_oa_related:snippet_body'] = array(
'type' => 'list<text>',
);
}
if (isset($data['node_index'])) {
$data['node_index']->options['fields']['field_oa_related'] = array(
'type' => 'list<integer>',
'entity_type' => 'paragraphs_item',
);
$data['node_index']->options['fields']['field_oa_related:field_paragraph_text:value'] = array(
'type' => 'list<text>',
);
$data['node_index']->options['fields']['field_oa_related:snippet_body'] = array(
'type' => 'list<text>',
);
}
}
function oa_related_template_modules_enabled($modules) {
foreach ($modules as $module_name) {
$node_infos = module_invoke($module_name, 'node_info');
if (array_intersect_key(oa_related_get_content_types(), $node_infos)) {
$cache =& drupal_static('features_get_default', array());
$cache = array();
features_template_revert('rebuild');
break;
}
}
}
function oa_related_paragraphs_field_allowed_values($field, $instance, $entity_type, $entity, $cacheable) {
$cache = cache_get(__FUNCTION__ . ':' . $instance['field_name'] . ':' . $instance['bundle'], 'cache_field');
if (!$cache) {
$info = oa_related_get_paragraph_field_info();
$values = array();
foreach ($info as $field_name => $bundle) {
if ($field_name == $instance['field_name']) {
foreach ($bundle as $name => $data) {
$values = $data['allowed values'];
drupal_alter('oa_related_' . $field_name . '_' . $name . '_values', $values);
}
}
}
cache_set(__FUNCTION__ . ':' . $instance['field_name'] . ':' . $instance['bundle'], $values, 'cache_field');
return $values;
}
else {
if (!empty($cache->data)) {
return $cache->data;
}
}
}
function oa_related_get_paragraph_field_info() {
$info =& drupal_static(__FUNCTION__);
if (empty($info)) {
$cache = cache_get(__FUNCTION__);
if (!$cache) {
$info = module_invoke_all('oa_related_field_info');
drupal_alter('oa_related_field_info', $info);
return $info;
}
else {
if (!empty($cache->data)) {
return $cache->data;
}
}
}
return $info;
}
function oa_related_oa_related_icons($type) {
$info = array();
switch ($type) {
case 'paragraph_content':
$info = array(
'icon' => decode_entities(''),
);
break;
case 'paragraph_media':
$info = array(
'icon' => decode_entities(''),
);
break;
case 'paragraph_snippet':
$info = array(
'icon' => decode_entities(''),
);
break;
case 'paragraph_text':
$info = array(
'icon' => decode_entities(''),
);
break;
default:
break;
}
return $info;
}