first import

This commit is contained in:
Bachir Soussi Chiadmi
2015-04-08 11:40:19 +02:00
commit 1bc61b12ad
8435 changed files with 1582817 additions and 0 deletions

View File

@@ -0,0 +1,273 @@
<?php
/**
* @file
*
* Plugin to provide a node context. A node context is a node wrapped in a
* context object that can be utilized by anything that accepts contexts.
*/
/**
* Plugins are described by creating a $plugin array which will be used
* by the system that includes this file.
*/
$plugin = array(
'title' => t("Entity"),
'description' => t('Entity object.'),
'context' => 'ctools_context_create_entity',
'edit form' => 'ctools_context_entity_settings_form',
'defaults' => array('entity_id' => ''),
'convert list' => 'ctools_context_entity_convert_list',
'convert' => 'ctools_context_entity_convert',
'placeholder form' => array(
'#type' => 'textfield',
'#description' => t('Enter the ID of an entity for this context.'),
),
'get child' => 'ctools_context_entity_get_child',
'get children' => 'ctools_context_entity_get_children',
);
function ctools_context_entity_get_child($plugin, $parent, $child) {
$plugins = ctools_context_entity_get_children($plugin, $parent);
return $plugins[$parent . ':' . $child];
}
function ctools_context_entity_get_children($plugin, $parent) {
$entities = entity_get_info();
$plugins = array();
foreach ($entities as $entity_type => $entity) {
$child_plugin = $plugin;
$child_plugin['title'] = $entity['label'];
$child_plugin['keyword'] = $entity_type;
$child_plugin['context name'] = $entity_type;
$child_plugin['name'] = $parent . ':' . $entity_type;
$child_plugin['description'] = t('Creates @entity context from an entity ID.', array('@entity' => $entity_type));
$child_plugin_id = $parent . ':' . $entity_type;
drupal_alter('ctools_entity_context', $child_plugin, $entity, $child_plugin_id);
$plugins[$child_plugin_id] = $child_plugin;
}
drupal_alter('ctools_entity_contexts', $plugins);
return $plugins;
}
/**
* It's important to remember that $conf is optional here, because contexts
* are not always created from the UI.
*/
function ctools_context_create_entity($empty, $data = NULL, $conf = FALSE, $plugin) {
$entity_type = $plugin['keyword'];
$entity = entity_get_info($entity_type);
$context = new ctools_context(array('entity:' . $entity_type, 'entity', $entity_type));
$context->plugin = $plugin['name'];
$context->keyword = $entity_type;
if ($empty) {
return $context;
}
// Attempt to retain compatibility with broken id:
if (is_array($data) && !isset($data['entity_id']) && isset($data['id'])) {
$id = $data['id'];
}
elseif (is_array($data) && isset($data['entity_id'])) {
$id = $data['entity_id'];
}
elseif (is_object($data)) {
$ids = entity_extract_ids($entity_type, $data);
$id = $ids[0];
}
elseif (is_numeric($data)) {
$id = $data;
$data = entity_load($entity_type, array($id));
$data = !empty($data[$id]) ? $data[$id] : FALSE;
}
if (is_array($data)) {
$data = entity_load($entity_type, array($id));
$data = !empty($data[$id]) ? $data[$id] : FALSE;
}
if (!empty($data)) {
$context->data = $data;
if (!empty($entity['entity keys']['label'])) {
$context->title = $data->{$entity['entity keys']['label']};
}
$context->argument = $id;
if ($entity['entity keys']['bundle']) {
$context->restrictions['type'] = array($data->{$entity['entity keys']['bundle']});
}
return $context;
}
}
function ctools_context_entity_settings_form($form, &$form_state) {
$conf = &$form_state['conf'];
$plugin = &$form_state['plugin'];
$form['entity'] = array(
'#title' => t('Enter the title or ID of a @entity entity', array('@entity' => $plugin['keyword'])),
'#type' => 'textfield',
'#maxlength' => 512,
'#autocomplete_path' => 'ctools/autocomplete/' . $plugin['keyword'],
'#weight' => -10,
);
if (!empty($conf['entity_id'])) {
$info = entity_load($plugin['keyword'], array($conf['entity_id']));
$info = $info[$conf['entity_id']];
if ($info) {
$entity = entity_get_info($plugin['keyword']);
$uri = entity_uri($plugin['keyword'], $info);
if (is_array($uri) && $entity['entity keys']['label']) {
$link = l(t("'%title' [%type id %id]", array('%title' => $info->{$entity['entity keys']['label']}, '%type' => $plugin['keyword'], '%id' => $conf['entity_id'])), $uri['path'], array('attributes' => array('target' => '_blank', 'title' => t('Open in new window')), 'html' => TRUE));
}
elseif (is_array($uri)) {
$link = l(t("[%type id %id]", array('%type' => $plugin['keyword'], '%id' => $conf['entity_id'])), $uri['path'], array('attributes' => array('target' => '_blank', 'title' => t('Open in new window')), 'html' => TRUE));
}
elseif ($entity['entity keys']['label']) {
$link = l(t("'%title' [%type id %id]", array('%title' => $info->{$entity['entity keys']['label']}, '%type' => $plugin['keyword'], '%id' => $conf['entity_id'])), file_create_url($uri), array('attributes' => array('target' => '_blank', 'title' => t('Open in new window')), 'html' => TRUE));
}
else {
$link = t("[%type id %id]", array('%type' => $plugin['keyword'], '%id' => $conf['entity_id']));
}
$form['entity']['#description'] = t('Currently set to !link', array('!link' => $link));
}
}
$form['entity_id'] = array(
'#type' => 'value',
'#value' => $conf['entity_id'],
);
$form['entity_type'] = array(
'#type' => 'value',
'#value' => $plugin['keyword'],
);
$form['set_identifier'] = array(
'#type' => 'checkbox',
'#default_value' => FALSE,
'#title' => t('Reset identifier to entity label'),
'#description' => t('If checked, the identifier will be reset to the entity label of the selected entity.'),
);
return $form;
}
/**
* Validate a node.
*/
function ctools_context_entity_settings_form_validate($form, &$form_state) {
// Validate the autocomplete
if (empty($form_state['values']['entity_id']) && empty($form_state['values']['entity'])) {
form_error($form['entity'], t('You must select an entity.'));
return;
}
if (empty($form_state['values']['entity'])) {
return;
}
$id = $form_state['values']['entity'];
$preg_matches = array();
$match = preg_match('/\[id: (\d+)\]/', $id, $preg_matches);
if (!$match) {
$match = preg_match('/^id: (\d+)/', $id, $preg_matches);
}
if ($match) {
$id = $preg_matches[1];
}
if (is_numeric($id)) {
$entity = entity_load($form_state['values']['entity_type'], array($id));
$entity = $entity[$id];
}
else {
$entity_info = entity_get_info($form_state['values']['entity_type']);
$field = $entity_info['entity keys']['label'];
$entity = entity_load($form_state['values']['entity_type'], FALSE, array($field => $id));
}
// Do not allow unpublished nodes to be selected by unprivileged users
// || (empty($node->status) && !(user_access('administer nodes'))) need a new sanity check at some point.
if (!$entity) {
form_error($form['entity'], t('Invalid entity selected.'));
}
else {
$entity_id = entity_extract_ids($form_state['values']['entity_type'], $entity);
form_set_value($form['entity_id'], $entity_id[0], $form_state);
}
}
function ctools_context_entity_settings_form_submit($form, &$form_state) {
if ($form_state['values']['set_identifier']) {
$entity_info = entity_get_info($form_state['values']['entity_type']);
$entity = entity_load($form_state['values']['entity_type'], array($form_state['values']['entity_id']));
$entity = $entity[$form_state['values']['entity_id']];
$form_state['values']['identifier'] = $entity->{$entity_info['entity keys']['label']};
}
// This will either be the value set previously or a value set by the
// validator.
$form_state['conf']['entity_id'] = $form_state['values']['entity_id'];
}
/**
* Provide a list of ways that this context can be converted to a string.
*/
function ctools_context_entity_convert_list($plugin) {
$list = array();
$entity = entity_get_info($plugin['context name']);
if (isset($entity['token type'])) {
$token = $entity['token type'];
}
else {
$token = $plugin['context name'];
}
// Hack: we need either token.module or a core fix for this to work right,
// until then, we just muscle it.
if ($token == 'taxonomy_term') {
$token = 'term';
}
$tokens = token_info();
if (isset($tokens['tokens'][$token])) {
foreach ($tokens['tokens'][$token] as $id => $info) {
if (!isset($list[$id])) {
$list[$id] = $info['name'];
}
}
}
return $list;
}
/**
* Convert a context into a string.
*/
function ctools_context_entity_convert($context, $type, $options = array()) {
$entity_type = $context->type[2];
$entity = entity_get_info($entity_type);
if (isset($entity['token type'])) {
$token = $entity['token type'];
}
else {
$token = $entity_type;
}
// Hack: we need either token.module or a core fix for this to work right,
// until then, we just muscle it.
if ($token == 'taxonomy_term') {
$token = 'term';
}
$tokens = token_info();
$values = token_generate($token, array($type => $type), array($token => $context->data), $options);
if (isset($values[$type])) {
return $values[$type];
}
}

View File

@@ -0,0 +1,181 @@
<?php
/**
* @file
*
* Plugin to provide a node context. A node context is a node wrapped in a
* context object that can be utilized by anything that accepts contexts.
*/
/**
* Plugins are described by creating a $plugin array which will be used
* by the system that includes this file.
*/
$plugin = array(
'title' => t("Node"),
'description' => t('A node object.'),
'context' => 'ctools_context_create_node',
'edit form' => 'ctools_context_node_settings_form',
'defaults' => array('nid' => ''),
'keyword' => 'node',
'context name' => 'node',
'convert list' => 'ctools_context_node_convert_list',
'convert' => 'ctools_context_node_convert',
'placeholder form' => array(
'#type' => 'textfield',
'#description' => t('Enter the node ID of a node for this context.'),
),
// This context is deprecated and should not be usable in the UI.
'no ui' => TRUE,
'no required context ui' => TRUE,
'superceded by' => 'entity:node',
);
/**
* It's important to remember that $conf is optional here, because contexts
* are not always created from the UI.
*/
function ctools_context_create_node($empty, $data = NULL, $conf = FALSE) {
$context = new ctools_context('node');
$context->plugin = 'node';
if ($empty) {
return $context;
}
if ($conf) {
$nid = is_array($data) && isset($data['nid']) ? $data['nid'] : (is_object($data) ? $data->nid : 0);
if (module_exists('translation')) {
if ($translation = module_invoke('translation', 'node_nid', $nid, $GLOBALS['language']->language)) {
$nid = $translation;
$reload = TRUE;
}
}
if (is_array($data) || !empty($reload)) {
$data = node_load($nid);
}
}
if (!empty($data)) {
$context->data = $data;
$context->title = $data->title;
$context->argument = $data->nid;
$context->restrictions['type'] = array($data->type);
return $context;
}
}
function ctools_context_node_settings_form($form, &$form_state) {
$conf = &$form_state['conf'];
$form['node'] = array(
'#title' => t('Enter the title or NID of a node'),
'#type' => 'textfield',
'#maxlength' => 512,
'#autocomplete_path' => 'ctools/autocomplete/node',
'#weight' => -10,
);
if (!empty($conf['nid'])) {
$info = db_query('SELECT * FROM {node} WHERE nid = :nid', array(':nid' => $conf['nid']))->fetchObject();
if ($info) {
$link = l(t("'%title' [node id %nid]", array('%title' => $info->title, '%nid' => $info->nid)), "node/$info->nid", array('attributes' => array('target' => '_blank', 'title' => t('Open in new window')), 'html' => TRUE));
$form['node']['#description'] = t('Currently set to !link', array('!link' => $link));
}
}
$form['nid'] = array(
'#type' => 'value',
'#value' => $conf['nid'],
);
$form['set_identifier'] = array(
'#type' => 'checkbox',
'#default_value' => FALSE,
'#title' => t('Reset identifier to node title'),
'#description' => t('If checked, the identifier will be reset to the node title of the selected node.'),
);
return $form;
}
/**
* Validate a node.
*/
function ctools_context_node_settings_form_validate($form, &$form_state) {
// Validate the autocomplete
if (empty($form_state['values']['nid']) && empty($form_state['values']['node'])) {
form_error($form['node'], t('You must select a node.'));
return;
}
if (empty($form_state['values']['node'])) {
return;
}
$nid = $form_state['values']['node'];
$preg_matches = array();
$match = preg_match('/\[id: (\d+)\]/', $nid, $preg_matches);
if (!$match) {
$match = preg_match('/^id: (\d+)/', $nid, $preg_matches);
}
if ($match) {
$nid = $preg_matches[1];
}
if (is_numeric($nid)) {
$node = db_query('SELECT nid, status FROM {node} WHERE nid = :nid', array(':nid' => $nid))->fetchObject();
}
else {
$node = db_query('SELECT nid, status FROM {node} WHERE LOWER(title) = LOWER(:title)', array(':title' => $nid))->fetchObject();
}
// Do not allow unpublished nodes to be selected by unprivileged users
if (!$node || (empty($node->status) && !(user_access('administer nodes')))) {
form_error($form['node'], t('Invalid node selected.'));
}
else {
form_set_value($form['nid'], $node->nid, $form_state);
}
}
function ctools_context_node_settings_form_submit($form, &$form_state) {
if ($form_state['values']['set_identifier']) {
$node = node_load($form_state['values']['nid']);
$form_state['values']['identifier'] = $node->title;
}
// This will either be the value set previously or a value set by the
// validator.
$form_state['conf']['nid'] = $form_state['values']['nid'];
}
/**
* Provide a list of ways that this context can be converted to a string.
*/
function ctools_context_node_convert_list() {
$tokens = token_info();
foreach ($tokens['tokens']['node'] as $id => $info) {
if (!isset($list[$id])) {
$list[$id] = $info['name'];
}
}
return $list;
}
/**
* Convert a context into a string.
*/
function ctools_context_node_convert($context, $type) {
$tokens = token_info();
if (isset($tokens['tokens']['node'][$type])) {
$values = token_generate('node', array($type => $type), array('node' => $context->data));
if (isset($values[$type])) {
return $values[$type];
}
}
}

View File

@@ -0,0 +1,124 @@
<?php
/**
* @file
*
* Plugin to provide a node_add_form context
*/
/**
* Plugins are described by creating a $plugin array which will be used
* by the system that includes this file.
*/
$plugin = array(
'title' => t('Node add form'),
'description' => t('A node add form.'),
'context' => 'ctools_context_create_node_add_form',
'edit form' => 'ctools_context_node_add_form_settings_form',
'defaults' => array('type' => ''),
'keyword' => 'node_add',
'context name' => 'node_add_form',
'convert list' => array('type' => t('Node type')),
'convert' => 'ctools_context_node_add_form_convert',
'placeholder form' => array(
'#type' => 'textfield',
'#description' => t('Enter the node type this context.'),
),
);
/**
* It's important to remember that $conf is optional here, because contexts
* are not always created from the UI.
*/
function ctools_context_create_node_add_form($empty, $data = NULL, $conf = FALSE) {
static $creating = FALSE;
$context = new ctools_context(array('form', 'node_add', 'node_form', 'node', 'entity:node'));
$context->plugin = 'node_add_form';
if ($empty || ($creating)) {
return $context;
}
$creating = TRUE;
if ($conf && (isset($data['types']) || isset($data['type']))) {
// Holdover from typo'd config.
$data = isset($data['types']) ? $data['types'] : $data['type'];
}
if (!empty($data)) {
$types = node_type_get_types();
$type = str_replace('-', '_', $data);
// Validate the node type exists.
if (isset($types[$type]) && node_access('create', $type)) {
// Initialize settings:
global $user;
$node = (object) array(
'uid' => $user->uid,
'name' => (isset($user->name) ? $user->name : ''),
'type' => $type,
'language' => LANGUAGE_NONE,
);
$form_id = $type . '_node_form';
$form_state = array(
'want form' => TRUE,
'build_info' => array(
'args' => array($node)
)
);
// Use module_load_include so that caches and stuff can know to load this.
form_load_include($form_state, 'inc', 'node', 'node.pages');
$form = drupal_build_form($form_id, $form_state);
// In a form, $data is the object being edited.
$context->data = $node;
$context->title = $types[$type]->name;
$context->argument = $type;
// These are specific pieces of data to this form.
// All forms should place the form here.
$context->form = $form;
$context->form_id = $form_id;
$context->form_title = t('Submit @name', array('@name' => $types[$type]->name));
$context->node_type = $type;
$context->restrictions['type'] = array($type);
$context->restrictions['form'] = array('form');
$creating = FALSE;
return $context;
}
}
$creating = FALSE;
}
function ctools_context_node_add_form_settings_form($form, &$form_state) {
$conf = $form_state['conf'];
$form['type'] = array(
'#title' => t('Node type'),
'#type' => 'select',
'#options' => node_type_get_names(),
'#default_value' => $conf['type'],
'#description' => t('Select the node type for this form.'),
);
return $form;
}
function ctools_context_node_add_form_settings_form_submit($form, &$form_state) {
$form_state['conf']['type'] = $form_state['values']['type'];
}
/**
* Convert a context into a string.
*/
function ctools_context_node_add_form_convert($context, $type) {
switch ($type) {
case 'type':
return $context->data->type;
}
}

View File

@@ -0,0 +1,192 @@
<?php
/**
* @file
*
* Plugin to provide a node_edit_form context
*/
/**
* Plugins are described by creating a $plugin array which will be used
* by the system that includes this file.
*/
$plugin = array(
'title' => t("Node edit form"),
'description' => t('A node edit form.'),
'context' => 'ctools_context_create_node_edit_form',
'edit form' => 'ctools_context_node_edit_form_settings_form',
'defaults' => array('nid' => ''),
'keyword' => 'node_edit',
'context name' => 'node_edit_form',
'convert list' => 'ctools_context_node_edit_convert_list',
'convert' => 'ctools_context_node_edit_convert',
'placeholder form' => array(
'#type' => 'textfield',
'#description' => t('Enter the node ID of a node for this argument:'),
),
);
/**
* It's important to remember that $conf is optional here, because contexts
* are not always created from the UI.
*/
function ctools_context_create_node_edit_form($empty, $node = NULL, $conf = FALSE) {
static $creating = FALSE;
$context = new ctools_context(array('form', 'node_edit', 'node_form', 'node_edit_form', 'node', 'entity:node'));
$context->plugin = 'node_edit_form';
if ($empty || ($creating)) {
return $context;
}
$creating = TRUE;
if ($conf) {
// In this case, $node is actually our $conf array.
$nid = is_array($node) && isset($node['nid']) ? $node['nid'] : (is_object($node) ? $node->nid : 0);
if (module_exists('translation')) {
if ($translation = module_invoke('translation', 'node_nid', $nid, $GLOBALS['language']->language)) {
$nid = $translation;
$reload = TRUE;
}
}
if (is_array($node) || !empty($reload)) {
$node = node_load($nid);
}
}
if (!empty($node)) {
$form_id = $node->type . '_node_form';
$form_state = array('want form' => TRUE, 'build_info' => array('args' => array($node)));
$file = drupal_get_path('module', 'node') . '/node.pages.inc';
require_once DRUPAL_ROOT . '/' . $file;
// This piece of information can let other modules know that more files
// need to be included if this form is loaded from cache:
$form_state['build_info']['files'] = array($file);
$form = drupal_build_form($form_id, $form_state);
// Fill in the 'node' portion of the context
$context->data = $node;
$context->title = isset($node->title) ? $node->title : '';
$context->argument = isset($node->nid) ? $node->nid : $node->type;
$context->form = $form;
$context->form_state = &$form_state;
$context->form_id = $form_id;
$context->form_title = isset($node->title) ? $node->title : '';
$context->node_type = $node->type;
$context->restrictions['type'] = array($node->type);
$context->restrictions['form'] = array('form');
$creating = FALSE;
return $context;
}
$creating = FALSE;
}
function ctools_context_node_edit_form_settings_form($form, &$form_state) {
$conf = &$form_state['conf'];
$form['node'] = array(
'#title' => t('Enter the title or NID of a node'),
'#type' => 'textfield',
'#maxlength' => 512,
'#autocomplete_path' => 'ctools/autocomplete/node',
'#weight' => -10,
);
if (!empty($conf['nid'])) {
$info = db_query('SELECT * FROM {node} WHERE nid = :nid', array(':nid' => $conf['nid']))->fetchObject();
if ($info) {
$link = l(t("'%title' [node id %nid]", array('%title' => $info->title, '%nid' => $info->nid)), "node/$info->nid", array('attributes' => array('target' => '_blank', 'title' => t('Open in new window')), 'html' => TRUE));
$form['node']['#description'] = t('Currently set to !link', array('!link' => $link));
}
}
$form['nid'] = array(
'#type' => 'value',
'#value' => $conf['nid'],
);
$form['set_identifier'] = array(
'#type' => 'checkbox',
'#default_value' => FALSE,
'#title' => t('Reset identifier to node title'),
'#description' => t('If checked, the identifier will be reset to the node title of the selected node.'),
);
return $form;
}
/**
* Validate a node.
*/
function ctools_context_node_edit_form_settings_form_validate($form, &$form_state) {
// Validate the autocomplete
if (empty($form_state['values']['nid']) && empty($form_state['values']['node'])) {
form_error($form['node'], t('You must select a node.'));
return;
}
if (empty($form_state['values']['node'])) {
return;
}
$nid = $form_state['values']['node'];
$preg_matches = array();
$match = preg_match('/\[id: (\d+)\]/', $nid, $preg_matches);
if (!$match) {
$match = preg_match('/^id: (\d+)/', $nid, $preg_matches);
}
if ($match) {
$nid = $preg_matches[1];
}
if (is_numeric($nid)) {
$node = db_query('SELECT nid, status FROM {node} WHERE nid = :nid', array(':nid' => $nid))->fetchObject();
}
else {
$node = db_query('SELECT nid, status FROM {node} WHERE LOWER(title) = LOWER(:title)', array(':title' => $nid))->fetchObject();
}
// Do not allow unpublished nodes to be selected by unprivileged users
if (!$node || (empty($node->status) && !(user_access('administer nodes')))) {
form_error($form['node'], t('Invalid node selected.'));
}
else {
form_set_value($form['nid'], $node->nid, $form_state);
}
}
function ctools_context_node_edit_form_settings_form_submit($form, &$form_state) {
if ($form_state['values']['set_identifier']) {
$node = node_load($form_state['values']['nid']);
$form_state['values']['identifier'] = $node->title;
}
// This will either be the value set previously or a value set by the
// validator.
$form_state['conf']['nid'] = $form_state['values']['nid'];
}
/**
* Provide a list of ways that this context can be converted to a string.
*/
function ctools_context_node_edit_convert_list() {
// Pass through to the "node" context convert list.
$plugin = ctools_get_context('node');
return ctools_context_node_convert_list();
}
/**
* Convert a context into a string.
*/
function ctools_context_node_edit_convert($context, $type) {
// Pass through to the "node" context convert list.
$plugin = ctools_get_context('node');
return ctools_context_node_convert($context, $type);
}

View File

@@ -0,0 +1,64 @@
<?php
/**
* @file
*
* Plugin to provide a string context
*/
/**
* Plugins are described by creating a $plugin array which will be used
* by the system that includes this file.
*/
$plugin = array(
'title' => t('String'),
'description' => t('A context that is just a string.'),
'context' => 'ctools_context_create_string',
'keyword' => 'string',
'no ui' => FALSE,
'context name' => 'string',
'convert list' => array(
'raw' => t('Raw string'),
'html_safe' => t('HTML-safe string'),
),
'convert' => 'ctools_context_string_convert',
'placeholder form' => array(
'#type' => 'textfield',
'#description' => t('Enter the string for this context.'),
),
);
/**
* It's important to remember that $conf is optional here, because contexts
* are not always created from the UI.
*/
function ctools_context_create_string($empty, $data = NULL, $conf = FALSE) {
// The input is expected to be an object as created by ctools_break_phrase
// which contains a group of string.
$context = new ctools_context('string');
$context->plugin = 'string';
if ($empty) {
return $context;
}
if ($data !== FALSE ) {
$context->data = $data;
$context->title = ($conf) ? check_plain($data['identifier']) : check_plain($data);
return $context;
}
}
/**
* Convert a context into a string.
*/
function ctools_context_string_convert($context, $type) {
switch ($type) {
case 'raw':
return $context->data;
case 'html_safe':
return check_plain($context->data);
}
}

View File

@@ -0,0 +1,166 @@
<?php
/**
* @file
*
* Plugin to provide a term context
*/
/**
* Plugins are described by creating a $plugin array which will be used
* by the system that includes this file.
*/
$plugin = array(
'title' => t("Taxonomy term"),
'description' => t('A single taxonomy term object.'),
'context' => 'ctools_context_create_term',
'edit form' => 'ctools_context_term_settings_form',
'defaults' => array(
'vid' => '',
'tid' => '',
),
'keyword' => 'term',
'context name' => 'term',
'convert list' => array(
'tid' => t('Term ID'),
'name' => t('Term name'),
'name_dashed' => t('Term name, lowercased and spaces converted to dashes'),
'description' => t('Term Description'),
'vid' => t('Vocabulary ID'),
),
'convert' => 'ctools_context_term_convert',
// This context is deprecated and should not be usable in the UI.
'no ui' => TRUE,
'no required context ui' => TRUE,
);
/**
* It's important to remember that $conf is optional here, because contexts
* are not always created from the UI.
*/
function ctools_context_create_term($empty, $data = NULL, $conf = FALSE) {
$context = new ctools_context('term');
$context->plugin = 'term';
if ($empty) {
return $context;
}
if ($conf && isset($data['tid'])) {
$data = taxonomy_term_load($data['tid']);
}
if (!empty($data)) {
$context->data = $data;
$context->title = $data->name;
$context->argument = $data->tid;
$context->description = $data->description;
return $context;
}
}
function ctools_context_term_settings_form($form, &$form_state) {
$conf = $form_state['conf'];
$form['vid'] = array(
'#title' => t('Vocabulary'),
'#type' => 'select',
'#options' => array(),
'#description' => t('Select the vocabulary for this form.'),
'#id' => 'ctools-select-vid',
'#default_value' => $conf['vid'],
);
$description = '';
if (!empty($conf['tid'])) {
$info = db_query('SELECT * FROM {taxonomy_term_data} WHERE tid = :tid', array(':tid' => $conf['tid']))->fetchObject();
if ($info) {
$description = ' ' . t('Currently set to @term. Enter another term if you wish to change the term.', array('@term' => $info->name));
}
}
ctools_include('dependent');
$options = array();
$form['taxonomy']['#tree'] = TRUE;
foreach (taxonomy_get_vocabularies() as $vid => $vocabulary) {
$options[$vid] = $vocabulary->name;
$form['taxonomy'][$vocabulary->vid] = array(
'#type' => 'textfield',
'#description' => t('Select a term from @vocabulary.', array('@vocabulary' => $vocabulary->name)) . $description,
'#autocomplete_path' => 'taxonomy/autocomplete/' . $vocabulary->vid,
'#dependency' => array('ctools-select-vid' => array($vocabulary->vid)),
);
}
$form['vid']['#options'] = $options;
$form['tid'] = array(
'#type' => 'value',
'#value' => $conf['tid'],
);
$form['set_identifier'] = array(
'#type' => 'checkbox',
'#default_value' => FALSE,
'#title' => t('Reset identifier to term title'),
'#description' => t('If checked, the identifier will be reset to the term name of the selected term.'),
);
return $form;
}
/**
* Validate a term.
*/
function ctools_context_term_settings_form_validate($form, &$form_state) {
// Validate the autocomplete
$vid = $form_state['values']['vid'];
if (empty($form_state['values']['tid']) && empty($form_state['values']['taxonomy'][$vid])) {
form_error($form['taxonomy'][$vid], t('You must select a term.'));
return;
}
if (empty($form_state['values']['taxonomy'][$vid])) {
return;
}
$term = db_query('SELECT tid FROM {taxonomy_term_data} WHERE LOWER(name) = LOWER(:name) AND vid = :vid', array(':name' => $form_state['values']['taxonomy'][$vid], ':vid' => $vid))->fetchObject();
if (!$term) {
form_error($form['taxonomy'][$vid], t('Invalid term selected.'));
}
else {
form_set_value($form['tid'], $term->tid, $form_state);
}
}
function ctools_context_term_settings_form_submit($form, &$form_state) {
if ($form_state['values']['set_identifier']) {
$term = db_query('SELECT tid, name FROM {taxonomy_term_data} WHERE LOWER(tid) = :tid', array(':tid' => $form_state['values']['tid']))->fetchObject();
$form_state['values']['identifier'] = $term->name;
}
$form_state['conf']['tid'] = $form_state['values']['tid'];
$form_state['conf']['vid'] = $form_state['values']['vid'];
}
/**
* Convert a context into a string.
*/
function ctools_context_term_convert($context, $type) {
switch ($type) {
case 'tid':
return $context->data->tid;
case 'name':
return $context->data->name;
case 'name_dashed':
return drupal_strtolower(str_replace(' ', '-', $context->data->name));
case 'vid':
return $context->data->vid;
case 'description':
return $context->data->description;
}
}

View File

@@ -0,0 +1,98 @@
<?php
/**
* @file
*
* Plugin to provide a terms context
*/
/**
* Plugins are described by creating a $plugin array which will be used
* by the system that includes this file.
*/
$plugin = array(
'title' => t("Taxonomy terms"),
'description' => t('Multiple taxonomy terms, as a group.'),
'context' => 'ctools_context_create_terms',
'keyword' => 'terms',
// This context is deprecated and should not be usable in the UI.
'no ui' => TRUE,
'context name' => 'terms',
'convert list' => array(
'tid' => t('Term ID of first term'),
'tids' => t('Term ID of all term, separated by + or ,'),
'name' => t('Term name of first term'),
'name_dashed' => t('Term name of first term, lowercased and spaces converted to dashes'),
'names' => t('Term name of all terms, separated by + or ,'),
'names_dashed' => t('Term name of all terms, separated by + or , and lowercased and spaces converted to dashes'),
'vid' => t('Vocabulary ID of first term'),
),
'convert' => 'ctools_context_terms_convert',
);
/**
* It's important to remember that $conf is optional here, because contexts
* are not always created from the UI.
*/
function ctools_context_create_terms($empty, $data = NULL, $conf = FALSE) {
// The input is expected to be an object as created by ctools_break_phrase
// which contains a group of terms.
$context = new ctools_context(array('terms', 'entity:taxonomy_term'));
$context->plugin = 'terms';
if ($empty) {
return $context;
}
if (!empty($data) && is_object($data)) {
$context->operator = $data->operator;
$context->tids = $data->value;
if (!isset($data->term)) {
// load the first term:
reset($context->tids);
$data->term = taxonomy_term_load(current($context->tids));
}
$context->data = $data->term;
$context->title = $data->term->name;
$context->argument = implode($context->operator == 'or' ? '+' : ',', array_unique($context->tids));
return $context;
}
}
/**
* Convert a context into a string.
*/
function ctools_context_terms_convert($context, $type) {
switch ($type) {
case 'tid':
return $context->data->tid;
case 'tids':
return $context->argument;
case 'name':
return $context->data->name;
case 'name_dashed':
return drupal_strtolower(str_replace(' ', '-', $context->data->name));
case 'names':
case 'names_dashed':
// We only run this query if this item was requested:
if (!isset($context->names)) {
if (empty($context->tids)) {
$context->names = '';
}
else {
$result = db_query('SELECT tid, name FROM {taxonomy_term_data} WHERE tid IN (:tids)', array(':tids' => $context->tids));
foreach ($result as $term) {
$names[$term->tid] = $term->name;
if ($type == 'names_dashed') {
$names[$term->tid] = drupal_strtolower(str_replace(' ', '-', $names[$term->tid]));
}
}
$context->names = implode($context->operator == 'or' ? ' + ' : ', ', $names);
}
}
return $context->names;
case 'vid':
return $context->data->vid;
}
}

View File

@@ -0,0 +1,61 @@
<?php
/**
* @file
* Provide a global context to allow for token support.
*/
$plugin = array(
'title' => t('Token'),
'description' => t('A context that contains token replacements from token.module.'),
'context' => 'ctools_context_create_token', // func to create context
'context name' => 'token',
'keyword' => 'token',
'convert list' => 'ctools_context_token_convert_list',
'convert' => 'ctools_context_token_convert',
);
/**
* Create a context from manual configuration.
*/
function ctools_context_create_token($empty, $data = NULL, $conf = FALSE) {
$context = new ctools_context('token');
$context->plugin = 'token';
return $context;
}
/**
* Implementation of hook_ctools_context_convert_list().
*/
function ctools_context_token_convert_list() {
$tokens = token_info();
foreach ($tokens['types'] as $type => $type_info) {
if (empty($type_info['needs-data'])) {
$real_type = isset($type_info['type']) ? $type_info['type'] : $type;
foreach ($tokens['tokens'][$real_type] as $id => $info) {
$key = "$type:$id";
if (!isset($list[$key])) {
$list[$key] = $type_info['name'] . ': ' . $info['name'];
}
}
}
}
return $list;
}
/**
* Implementation of hook_ctools_context_converter_alter().
*/
function ctools_context_token_convert($context, $token) {
$tokens = token_info();
list($type, $token) = explode(':', $token, 2);
$real_type = isset($tokens['types'][$type]['type']) ? $tokens['types'][$type]['type'] : $type;
if (isset($tokens['tokens'][$real_type][$token])) {
$values = token_generate($type, array($token => $token));
if (isset($values[$token])) {
return $values[$token];
}
}
}

View File

@@ -0,0 +1,174 @@
<?php
/**
* @file
*
* Plugin to provide a user context
*/
/**
* Plugins are described by creating a $plugin array which will be used
* by the system that includes this file.
*/
$plugin = array(
'title' => t("User"),
'description' => t('A single user object.'),
'context' => 'ctools_context_create_user',
'edit form' => 'ctools_context_user_settings_form',
'defaults' => array('type' => 'select', 'uid' => ''),
'keyword' => 'user',
'context name' => 'user',
'convert list' => 'ctools_context_user_convert_list',
'convert' => 'ctools_context_user_convert',
'convert default' => 'name',
// This context is deprecated and should not be usable in the UI.
'no ui' => TRUE,
'no required context ui' => TRUE,
);
/**
* It's important to remember that $conf is optional here, because contexts
* are not always created from the UI.
*/
function ctools_context_create_user($empty, $data = NULL, $conf = FALSE) {
$context = new ctools_context(array('entity:user', 'entity', 'user'));
$context->plugin = 'user';
if ($empty) {
return $context;
}
if ($conf) {
if ($data['type'] == 'current') {
global $user;
$data = user_load($user->uid);
$data->logged_in_user = TRUE;
}
else {
$data = user_load($data['uid']);
}
}
// Load entity if the data provided is a numeric value. This kind of data is
// passed by some relationships.
if (is_numeric($data)) {
$data = user_load($data);
}
if (!empty($data)) {
$context->data = $data;
$context->title = isset($data->name) ? $data->name : t('Anonymous');
$context->argument = $data->uid;
return $context;
}
}
function ctools_context_user_settings_form($form, &$form_state) {
$conf = $form_state['conf'];
ctools_include('dependent');
$form['type'] = array(
'#title' => t('Enter the context type'),
'#type' => 'radios',
'#options' => array(
'select' => t('Select a user'),
'current' => t('Logged in user'),
),
'#default_value' => $conf['type'],
);
$form['user'] = array(
'#title' => t('Enter a user name'),
'#type' => 'textfield',
'#maxlength' => 512,
'#autocomplete_path' => 'user/autocomplete',
'#dependency' => array('radio:type' => array('select')),
);
if (!empty($conf['uid'])) {
$info = user_load($conf['uid']);
if ($info) {
$form['user']['#description'] = t('Currently set to !link', array('!link' => theme('username', $info)));
}
}
$form['uid'] = array(
'#type' => 'value',
'#value' => $conf['uid'],
);
$form['set_identifier'] = array(
'#type' => 'checkbox',
'#default_value' => FALSE,
'#title' => t('Reset identifier to username'),
'#description' => t('If checked, the identifier will be reset to the user name of the selected user.'),
'#dependency' => array('radio:context[context_settings][type]' => array('select')),
);
return $form;
}
/**
* Validate a user.
*/
function ctools_context_user_settings_form_validate($form, &$form_state) {
if ($form_state['values']['type'] != 'select') {
return;
}
// Validate the autocomplete
if (empty($form_state['values']['uid']) && empty($form_state['values']['user'])) {
form_error($form['user'], t('You must select a user.'));
return;
}
if (empty($form_state['values']['user'])) {
return;
}
$account = user_load_by_name($form_state['values']['user']);
if (!$account) {
form_error($form['user'], t('Invalid user selected.'));
}
else {
form_set_value($form['uid'], $account->uid, $form_state);
}
}
function ctools_context_user_settings_form_submit($form, &$form_state) {
if ($form_state['values']['set_identifier']) {
$account = user_load($form_state['values']['uid']);
$form_state['values']['identifier'] = $account->name;
}
$form_state['conf']['type'] = $form_state['values']['type'];
$form_state['conf']['uid'] = $form_state['values']['uid'];
}
/**
* Provide a list of replacements.
*/
function ctools_context_user_convert_list() {
$tokens = token_info();
foreach ($tokens['tokens']['user'] as $id => $info) {
if (!isset($list[$id])) {
$list[$id] = $info['name'];
}
}
return $list;
}
/**
* Convert a context into a string.
*/
function ctools_context_user_convert($context, $type) {
$tokens = token_info();
if (isset($tokens['tokens']['user'][$type])) {
$values = token_generate('user', array($type => $type), array('user' => $context->data));
if (isset($values[$type])) {
return $values[$type];
}
}
}

View File

@@ -0,0 +1,192 @@
<?php
/**
* @file
*
* Plugin to provide a user_edit_form context
*/
/**
* Plugins are described by creating a $plugin array which will be used
* by the system that includes this file.
*/
$plugin = array(
'title' => t('User edit form'),
'description' => t('A user edit form.'),
'context' => 'ctools_context_create_user_edit_form',
'edit form' => 'ctools_context_user_edit_form_settings_form',
'defaults' => array('uid' => ''),
'keyword' => 'user_edit',
'context name' => 'user_edit_form',
'convert list' => 'ctools_context_user_edit_convert_list',
'convert' => 'ctools_context_user_edit_convert',
'placeholder form' => array(
'#type' => 'textfield',
'#description' => t('Enter the user ID of a user for this argument:'),
),
);
/**
* It's important to remember that $conf is optional here, because contexts
* are not always created from the UI.
*/
function ctools_context_create_user_edit_form($empty, $user = NULL, $conf = FALSE) {
// Determine the user category.
$category = !empty($conf['category']) ? $conf['category'] : FALSE;
unset($conf['category']);
// Return previously created contexts, per category.
static $created = array();
if (!empty($created[$category])) {
return $created[$category];
}
// If no category was specified, use the default 'account'.
if (!$category) {
$category = 'account';
}
$context = new ctools_context(array('form', 'user_edit', 'user_form', 'user_edit_form', 'user', 'entity:user'));
// Store this context for later.
$created[$category] = $context;
$context->plugin = 'user_edit_form';
if ($empty) {
return $context;
}
if (!empty($conf)) {
// In this case, $user is actually our $conf array.
$uid = is_array($user) && isset($user['uid']) ? $user['uid'] : (is_object($user) ? $user->uid : 0);
if (module_exists('translation')) {
if ($translation = module_invoke('translation', 'user_uid', $uid, $GLOBALS['language']->language)) {
$uid = $translation;
$reload = TRUE;
}
}
if (is_array($user) || !empty($reload)) {
$user = user_load($uid);
}
}
if (!empty($user)) {
$form_id = 'user_profile_form';
$form_state = array('want form' => TRUE, 'build_info' => array('args' => array($user, $category)));
$file = drupal_get_path('module', 'user') . '/user.pages.inc';
require_once DRUPAL_ROOT . '/' . $file;
// This piece of information can let other modules know that more files
// need to be included if this form is loaded from cache:
$form_state['build_info']['files'] = array($file);
$form = drupal_build_form($form_id, $form_state);
// Fill in the 'node' portion of the context
$context->data = $user;
$context->title = isset($user->name) ? $user->name : '';
$context->argument = $user->uid;
$context->form = $form;
$context->form_state = &$form_state;
$context->form_id = $form_id;
$context->form_title = isset($user->name) ? $user->name : '';
$context->restrictions['form'] = array('form');
return $context;
}
}
function ctools_context_user_edit_form_settings_form($form, &$form_state) {
$conf = &$form_state['conf'];
$form['user'] = array(
'#title' => t('Enter the name or UID of a node'),
'#type' => 'textfield',
'#maxlength' => 512,
'#autocomplete_path' => 'ctools/autocomplete/user',
'#weight' => -10,
);
if (!empty($conf['uid'])) {
$info = db_query('SELECT * FROM {user} WHERE uid = :uid', array(':uid' => $conf['uid']))->fetchObject();
if ($info) {
$link = l(t("'%name' [user id %uid]", array('%name' => $info->name, '%uid' => $info->uid)), "user/$info->uid", array('attributes' => array('target' => '_blank', 'title' => t('Open in new window')), 'html' => TRUE));
$form['user']['#description'] = t('Currently set to !link', array('!link' => $link));
}
}
$form['uid'] = array(
'#type' => 'value',
'#value' => $conf['uid'],
);
$form['set_identifier'] = array(
'#type' => 'checkbox',
'#default_value' => FALSE,
'#title' => t('Reset identifier to user name'),
'#description' => t('If checked, the identifier will be reset to the user name of the selected user.'),
);
return $form;
}
/**
* Validate a node.
*/
function ctools_context_user_edit_form_settings_form_validate($form, &$form_state) {
// Validate the autocomplete
if (empty($form_state['values']['uid']) && empty($form_state['values']['user'])) {
form_error($form['user'], t('You must select a user.'));
return;
}
if (empty($form_state['values']['user'])) {
return;
}
$uid = $form_state['values']['user'];
$preg_matches = array();
$match = preg_match('/\[id: (\d+)\]/', $uid, $preg_matches);
if (!$match) {
$match = preg_match('/^id: (\d+)/', $uid, $preg_matches);
}
if ($match) {
$uid = $preg_matches[1];
}
if (is_numeric($uid)) {
$user = db_query('SELECT uid FROM {user} WHEREuid = :uid', array(':uid' => $uid))->fetchObject();
}
else {
$user = db_query('SELECT uid FROM {user} WHERE LOWER(name) = LOWER(:name)', array(':name' => $uid))->fetchObject();
}
form_set_value($form['uid'], $user->uid, $form_state);
}
function ctools_context_user_edit_form_settings_form_submit($form, &$form_state) {
if ($form_state['values']['set_identifier']) {
$user = user_load($form_state['values']['uid']);
$form_state['values']['identifier'] = $user->name;
}
// This will either be the value set previously or a value set by the
// validator.
$form_state['conf']['uid'] = $form_state['values']['uid'];
}
/**
* Provide a list of ways that this context can be converted to a string.
*/
function ctools_context_user_edit_convert_list() {
// Pass through to the "node" context convert list.
$plugin = ctools_get_context('user');
return ctools_context_user_convert_list();
}
/**
* Convert a context into a string.
*/
function ctools_context_user_edit_convert($context, $type) {
// Pass through to the "node" context convert list.
$plugin = ctools_get_context('user');
return ctools_context_user_convert($context, $type);
}

View File

@@ -0,0 +1,72 @@
<?php
/**
* @file
*
* Plugin to provide a vocabulary context
*/
/**
* Plugins are described by creating a $plugin array which will be used
* by the system that includes this file.
*/
$plugin = array(
'title' => t("Taxonomy vocabulary"),
'description' => t('A single taxonomy vocabulary object.'),
'context' => 'ctools_context_create_vocabulary',
'edit form' => 'ctools_context_vocabulary_settings_form',
'defaults' => array('vid' => ''),
'keyword' => 'vocabulary',
'context name' => 'vocabulary',
// This context is deprecated and should not be usable in the UI.
'no ui' => TRUE,
'no required context ui' => TRUE,
'superceded by' => 'entity:taxonomy_vocabulary',
);
/**
* It's important to remember that $conf is optional here, because contexts
* are not always created from the UI.
*/
function ctools_context_create_vocabulary($empty, $data = NULL, $conf = FALSE) {
$context = new ctools_context('vocabulary');
$context->plugin = 'vocabulary';
if ($empty) {
return $context;
}
if ($conf && isset($data['vid'])) {
$data = taxonomy_vocabulary_load($data['vid']);
}
if (!empty($data)) {
$context->data = $data;
$context->title = $data->name;
$context->argument = $data->vid;
return $context;
}
}
function ctools_context_vocabulary_settings_form($form, &$form_state) {
$conf = $form_state['conf'];
$options = array();
foreach (taxonomy_get_vocabularies() as $vid => $vocabulary) {
$options[$vid] = $vocabulary->name;
}
$form['vid'] = array(
'#title' => t('Vocabulary'),
'#type' => 'select',
'#options' => $options,
'#default_value' => $conf['vid'],
'#description' => t('Select the vocabulary for this form.'),
);
return $form;
}
function ctools_context_vocabulary_settings_form_submit($form, &$form_state) {
$form_state['conf']['vid'] = $form_state['values']['vid'];
}