View source
<?php
require DRUPAL_ROOT . '/' . drupal_get_path('module', 'freelinking_prepopulate') . '/freelinking_prepopulate.utilities.inc';
function freelinking_prepopulate_help($path, $arg) {
if (!function_exists('advanced_help_hint_docs')) {
drupal_set_message(t('To use <strong>Freelinking prepopulate</strong>, you must install and enable the module <strong>!url</strong>.', array(
'!url' => l('Advanced help hint', 'https://www.drupal.org/project/advanced_help_hint'),
)), 'error', FALSE);
return;
}
if ($path == 'admin/help#freelinking_prepopulate') {
return '<p>' . t('This module provides plugins for Freelinking to prepopulate node create.') . '</p>' . '<p>' . advanced_help_hint_docs('freelinking_prepopulate', 'https://www.drupal.org/node/2468155', TRUE) . '</p>';
}
}
function freelinking_prepopulate_freelinking() {
$freelinking['freelinking_prepopulate'] = array(
'indicator' => '/^create(node)?$/',
'callback' => 'freelinking_prepopulate_node_callback',
'settings' => 'freelinking_prepopulate_node_settings',
'failover' => array(
'search',
),
'tip' => t('Links to a prepopulated node/add form.'),
);
return $freelinking;
}
function freelinking_prepopulate_node_callback($target, $plugin) {
if (isset($target['type'])) {
$node_type = check_plain($target['type']);
}
else {
$node_type = variable_get('freelinking_prepopulate_node_type', _freelinking_prepopulate_default_node_type());
}
if (!freelinking_get_conf('cache') && !user_access('create ' . $node_type . ' content')) {
switch (variable_get('freelinking_createnode_failover', 'none')) {
case 'search':
return array(
'failover' => 'search',
);
case 'error':
return array(
'failover' => 'error',
'message' => t('Access Denied to Create Missing Content'),
);
}
}
$url = 'node/add/' . str_replace('_', '-', $node_type);
$title = $target['text'] ? $target['text'] : $target['dest'];
$query['edit[title]'] = $target['dest'];
if (isset($target['other']['prepopulate']) && is_array($target['other']['prepopulate'])) {
foreach ($target['other']['prepopulate'] as $field => $value) {
$query[$field] = $value;
}
}
else {
$query = array_merge($query, freelinking_prepopulate_fields_from_page(variable_get('freelinking_prepopulate_node_advanced', array())));
}
$query_override = freelinking_prepopulate_fields_from_array('nodecreate', $target);
if (isset($query_override)) {
foreach ($query_override as $key => $value) {
if ($value) {
$query[$key] = $value;
}
}
}
return array(
$title,
$url,
array(
'query' => $query,
'attributes' => array(
'title' => t('Create node') . ' "' . $target['dest'] . '"',
),
),
);
}
function freelinking_prepopulate_node_settings() {
$form['freelinking_prepopulate_node_type'] = array(
'#type' => 'select',
'#title' => t('Default node type to create'),
'#default_value' => variable_get('freelinking_prepopulate_node_type', _freelinking_prepopulate_default_node_type()),
'#options' => node_type_get_names(),
'#description' => t('Which content type should be created by Freelinking?'),
);
$nodecreate = freelinking_prepopulate_list_fields('nodecreate');
if (isset($nodecreate)) {
foreach ($nodecreate as $key => $value) {
$options[$key] = $value['title'];
}
}
if (empty($options)) {
return $form;
}
$form['freelinking_prepopulate_node_advanced'] = array(
'#type' => 'checkboxes',
'#title' => t('Prepopulate options'),
'#default_value' => variable_get('freelinking_prepopulate_node_advanced', array()),
'#options' => $options,
'#description' => t('Prepopulate the new node with values from the <em>Linking Node</em>.'),
);
return $form;
}
function _freelinking_prepopulate_default_node_type() {
$types = array();
$types = node_type_get_names();
$types = array_keys($types);
return array_shift($types);
}