first import
This commit is contained in:
@@ -0,0 +1,66 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Plugin to provide an relationship handler for book parent.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Plugins are described by creating a $plugin array which will be used
|
||||
* by the system that includes this file.
|
||||
*/
|
||||
$plugin = array(
|
||||
'title' => t('Book parent'),
|
||||
'keyword' => 'book_parent',
|
||||
'description' => t('Adds a book parent from a node context.'),
|
||||
'required context' => new ctools_context_required(t('Node'), 'node'),
|
||||
'context' => 'ctools_book_parent_context',
|
||||
'edit form' => 'ctools_book_parent_settings_form',
|
||||
'defaults' => array('type' => 'top'),
|
||||
);
|
||||
|
||||
/**
|
||||
* Return a new context based on an existing context.
|
||||
*/
|
||||
function ctools_book_parent_context($context, $conf) {
|
||||
// If unset it wants a generic, unfilled context, which is just NULL.
|
||||
if (empty($context->data)) {
|
||||
return ctools_context_create_empty('node');
|
||||
}
|
||||
|
||||
if (isset($context->data->book)) {
|
||||
if ($conf['type'] == 'top') {
|
||||
$nid = $context->data->book['bid'];
|
||||
}
|
||||
else {
|
||||
// Just load the parent book.
|
||||
$item = book_link_load($context->data->book['plid']);
|
||||
$nid = $item['nid'];
|
||||
}
|
||||
|
||||
if (!empty($nid)) {
|
||||
// Load the node.
|
||||
$node = node_load($nid);
|
||||
// Generate the context.
|
||||
return ctools_context_create('node', $node);
|
||||
}
|
||||
}
|
||||
else {
|
||||
return ctools_context_create_empty('node');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Settings form for the relationship.
|
||||
*/
|
||||
function ctools_book_parent_settings_form($form, &$form_state) {
|
||||
$conf = $form_state['conf'];
|
||||
$form['type'] = array(
|
||||
'#type' => 'select',
|
||||
'#title' => t('Relationship type'),
|
||||
'#options' => array('parent' => t('Immediate parent'), 'top' => t('Top level book')),
|
||||
'#default_value' => $conf['type'],
|
||||
);
|
||||
|
||||
return $form;
|
||||
}
|
@@ -0,0 +1,215 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Plugin to provide an relationship handler for an entity from a field.
|
||||
*/
|
||||
|
||||
/**
|
||||
* 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('Creates an entity context from a foreign key on a field.'),
|
||||
'context' => 'ctools_entity_from_field_context',
|
||||
'edit form' => 'ctools_entity_from_field_edit_form',
|
||||
'get child' => 'ctools_entity_from_field_get_child',
|
||||
'get children' => 'ctools_entity_from_field_get_children',
|
||||
'defaults' => array('delta' => 0),
|
||||
);
|
||||
|
||||
function ctools_entity_from_field_get_child($plugin, $parent, $child) {
|
||||
$plugins = ctools_entity_from_field_get_children($plugin, $parent);
|
||||
return $plugins[$parent . ':' . $child];
|
||||
}
|
||||
|
||||
function ctools_entity_from_field_get_children($parent_plugin, $parent) {
|
||||
$cid = $parent_plugin['name'] . ':' . $parent;
|
||||
$cache = &drupal_static(__FUNCTION__);
|
||||
if (!empty($cache[$cid])) {
|
||||
return $cache[$cid];
|
||||
}
|
||||
|
||||
ctools_include('fields');
|
||||
$entities = entity_get_info();
|
||||
$plugins = array();
|
||||
$context_types = array();
|
||||
|
||||
// Get the schema information for every field.
|
||||
$fields_info = field_info_fields();
|
||||
foreach ($fields_info as $field_name => $field) {
|
||||
foreach ($field['bundles'] as $from_entity => $bundles) {
|
||||
foreach ($bundles as $bundle) {
|
||||
// There might be fields attached to bundles that are disabled (e.g. a
|
||||
// module that declared a node's content type, is now disabled), but the
|
||||
// field instance is still shown.
|
||||
if (!empty($entities[$from_entity]['bundles'][$bundle])) {
|
||||
$foreign_keys = ctools_field_foreign_keys($field_name);
|
||||
|
||||
foreach ($foreign_keys as $key => $info) {
|
||||
if (isset($info['table'])) {
|
||||
foreach ($entities as $to_entity => $to_entity_info) {
|
||||
$from_entity_info = $entities[$from_entity];
|
||||
// If somehow the bundle doesn't exist on the to-entity,
|
||||
// skip.
|
||||
if (!isset($from_entity_info['bundles'][$bundle])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (isset($to_entity_info['base table']) && $to_entity_info['base table'] == $info['table'] && array_keys($info['columns'], $to_entity_info['entity keys']['id'])) {
|
||||
$name = $field_name . '-' . $from_entity . '-' . $to_entity;
|
||||
$plugin_id = $parent . ':' . $name;
|
||||
|
||||
// Record the bundle for later.
|
||||
$context_types[$plugin_id]['types'][$bundle] = $from_entity_info['bundles'][$bundle]['label'];
|
||||
|
||||
// We check for every bundle; this plugin may already have
|
||||
// been created, so don't recreate it.
|
||||
if (!isset($plugins[$plugin_id])) {
|
||||
$plugin = $parent_plugin;
|
||||
$replacements = array(
|
||||
'@to_entity' => $to_entity_info['label'],
|
||||
'@from_entity' => $from_entity_info['label'],
|
||||
'@field_name' => $field_name,
|
||||
'@field_label' => ctools_field_label($field_name),
|
||||
);
|
||||
$plugin['title'] = t('@to_entity from @from_entity (on @from_entity: @field_label [@field_name])', $replacements);
|
||||
$plugin['keyword'] = $to_entity;
|
||||
$plugin['context name'] = $name;
|
||||
$plugin['name'] = $plugin_id;
|
||||
$plugin['description'] = t('Creates a @to_entity context from @from_entity using the @field_name field on @from_entity.', $replacements);
|
||||
$plugin['from entity'] = $from_entity;
|
||||
$plugin['to entity'] = $to_entity;
|
||||
$plugin['field name'] = $field_name;
|
||||
$plugin['join key'] = $key;
|
||||
$plugin['source key'] = current(array_keys($info['columns']));
|
||||
$plugin['parent'] = $parent;
|
||||
|
||||
$plugins[$plugin_id] = $plugin;
|
||||
|
||||
/*
|
||||
-- commented out until I figure out how to actually load the reverse properly.
|
||||
// Build the reverse
|
||||
$plugin = $parent_plugin;
|
||||
$name = $field_name . '-' . $from_entity . '-' . $to_entity . '-reverse';
|
||||
$plugin_id = $parent . ':' . $name;
|
||||
|
||||
$plugin['title'] = t('@from_entity from @to_entity (on @from_entity: @field_name)', $replacements);
|
||||
$plugin['keyword'] = $to_entity;
|
||||
$plugin['context name'] = $name;
|
||||
$plugin['name'] = $plugin_id;
|
||||
$plugin['description'] = t('Creates a @from_entity context from @to_entity using the @field_name field on @from_entity.', $replacements);
|
||||
|
||||
$plugin['from entity'] = $from_entity;
|
||||
$plugin['to entity'] = $to_entity;
|
||||
$plugin['field name'] = $field_name;
|
||||
$plugin['reverse'] = TRUE;
|
||||
$plugin['parent'] = $parent;
|
||||
|
||||
// Since we can't apply restrictions on the reverse relationship
|
||||
// we just add the required context here.
|
||||
$plugin['required context'] = new ctools_context_required($to_entity_info['label'], $to_entity);
|
||||
|
||||
$plugin_entities = array(
|
||||
'to' => array($from_entity => $from_entity_info),
|
||||
'from' => array($to_entity => $to_entity_info)
|
||||
);
|
||||
drupal_alter('ctools_entity_context', $plugin, $plugin_entities, $plugin_id);
|
||||
|
||||
$plugins[$plugin_id] = $plugin;
|
||||
*/
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($context_types as $key => $context) {
|
||||
list($parent, $plugin_name) = explode(':', $key);
|
||||
list($field_name, $from_entity, $to_entity) = explode('-', $plugin_name);
|
||||
|
||||
$from_entity_info = $entities[$from_entity];
|
||||
$to_entity_info = $entities[$to_entity];
|
||||
|
||||
$plugins[$key]['required context'] = new ctools_context_required($from_entity_info['label'], $from_entity, array('type' => array_keys($context['types'])));
|
||||
|
||||
$plugin_entities = array(
|
||||
'to' => array($to_entity => $to_entity_info),
|
||||
'from' => array($from_entity => $from_entity_info)
|
||||
);
|
||||
drupal_alter('ctools_entity_context', $plugins[$key], $plugin_entities, $key);
|
||||
}
|
||||
drupal_alter('ctools_entity_contexts', $plugins);
|
||||
|
||||
$cache[$cid] = $plugins;
|
||||
return $plugins;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a new context based on an existing context.
|
||||
*/
|
||||
function ctools_entity_from_field_context($context, $conf) {
|
||||
$delta = !empty($conf['delta']) ? intval($conf['delta']) : 0;
|
||||
$plugin = $conf['name'];
|
||||
list($plugin, $plugin_name) = explode(':', $plugin);
|
||||
list($field_name, $from_entity, $to_entity) = explode('-', $plugin_name);
|
||||
// If unset it wants a generic, unfilled context, which is just NULL.
|
||||
$entity_info = entity_get_info($from_entity);
|
||||
if (empty($context->data) || !isset($context->data->{$entity_info['entity keys']['id']})) {
|
||||
return ctools_context_create_empty('entity:' . $to_entity, NULL);
|
||||
}
|
||||
|
||||
if (isset($context->data->{$entity_info['entity keys']['id']})) {
|
||||
// Load the entity.
|
||||
$id = $context->data->{$entity_info['entity keys']['id']};
|
||||
$entity = entity_load($from_entity, array($id));
|
||||
$entity = $entity[$id];
|
||||
if ($items = field_get_items($from_entity, $entity, $field_name)) {
|
||||
if (isset($items[$delta])) {
|
||||
ctools_include('fields');
|
||||
$to_entity_info = entity_get_info($to_entity);
|
||||
$plugin_info = ctools_get_relationship($conf['name']);
|
||||
$to_entity_id = $items[$delta][$plugin_info['source key']];
|
||||
|
||||
// Send it to ctools.
|
||||
return ctools_context_create('entity:' . $to_entity, $to_entity_id);
|
||||
}
|
||||
else {
|
||||
// In case that delta was empty.
|
||||
return ctools_context_create_empty('entity:' . $to_entity, NULL);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function ctools_entity_from_field_edit_form($form, &$form_state) {
|
||||
$field = field_info_field($form_state['plugin']['field name']);
|
||||
$conf = $form_state['conf'];
|
||||
|
||||
if ($field && $field['cardinality'] != 1) {
|
||||
if ($field['cardinality'] == -1) {
|
||||
$form['delta'] = array(
|
||||
'#type' => 'textfield',
|
||||
'#title' => t('Delta'),
|
||||
'#description' => t('The relationship can only create one context, but multiple items can be related. Please select which one. Since this can have unlimited items, type in the number you want. The first one will be 0.'),
|
||||
'#default_value' => !empty($conf['delta']) ? $conf['delta'] : 0,
|
||||
);
|
||||
}
|
||||
else {
|
||||
$form['delta'] = array(
|
||||
'#type' => 'select',
|
||||
'#title' => t('Delta'),
|
||||
'#description' => t('The relationship can only create one context, but multiple items can be related. Please select which one.'),
|
||||
'#options' => range(1, $field['cardinality']),
|
||||
'#default_value' => !empty($conf['delta']) ? $conf['delta'] : 0,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
return $form;
|
||||
}
|
@@ -0,0 +1,136 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Plugin to provide an relationship handler for an entity from a field.
|
||||
*/
|
||||
|
||||
/**
|
||||
* 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('Creates an entity context from a foreign key on a field.'),
|
||||
'context' => 'ctools_entity_from_schema_context',
|
||||
'get child' => 'ctools_entity_from_schema_get_child',
|
||||
'get children' => 'ctools_entity_from_schema_get_children',
|
||||
);
|
||||
|
||||
function ctools_entity_from_schema_get_child($plugin, $parent, $child) {
|
||||
$plugins = ctools_entity_from_schema_get_children($plugin, $parent);
|
||||
return $plugins[$parent . ':' . $child];
|
||||
}
|
||||
|
||||
function ctools_entity_from_schema_get_children($parent_plugin, $parent) {
|
||||
$entities = entity_get_info();
|
||||
$plugins = array();
|
||||
|
||||
foreach (module_implements('entity_info') as $module) {
|
||||
module_load_install($module);
|
||||
$schemas = drupal_get_schema();
|
||||
|
||||
foreach ($entities as $from_entity => $from_entity_info) {
|
||||
if (empty($from_entity_info['base table'])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$table = $from_entity_info['base table'];
|
||||
if (isset($schemas[$table]['foreign keys'])) {
|
||||
foreach ($schemas[$table]['foreign keys'] as $relationship => $info) {
|
||||
foreach ($entities as $to_entity => $to_entity_info) {
|
||||
if (empty($info['table'])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (isset($to_entity_info['base table']) && $info['table'] == $to_entity_info['base table'] && in_array($to_entity_info['entity keys']['id'], $info['columns'])) {
|
||||
$this_col = ctools_entity_from_schema_columns_filter($info['columns'], $to_entity_info['entity keys']['id']);
|
||||
|
||||
// Set up our t() replacements as we reuse these.
|
||||
$replacements = array(
|
||||
'@relationship' => $relationship,
|
||||
'@base_table' => $table,
|
||||
'@to_entity' => $to_entity_info['label'],
|
||||
'@from_entity' => $from_entity_info['label'],
|
||||
);
|
||||
|
||||
$name = $this_col . '-' . $from_entity . '-' . $to_entity;
|
||||
$plugin_id = $parent . ':' . $name;
|
||||
$plugin = $parent_plugin;
|
||||
|
||||
$plugin['title'] = t('@to_entity from @from_entity (on @base_table.@relationship)', $replacements);
|
||||
$plugin['keyword'] = $to_entity;
|
||||
$plugin['context name'] = $name;
|
||||
$plugin['name'] = $plugin_id;
|
||||
$plugin['description'] = t('Builds a relationship from a @from_entity to a @to_entity using the @base_table.@relationship field.', $replacements);
|
||||
|
||||
$plugin['required context'] = new ctools_context_required($from_entity_info['label'], $from_entity);
|
||||
|
||||
$plugin_entities = array('to' => $to_entity_info, 'from' => $from_entity_info);
|
||||
$plugin_entities = array('to' => array($to_entity => $to_entity_info), 'from' => array($from_entity => $from_entity_info));
|
||||
|
||||
drupal_alter('ctools_entity_context', $plugin, $plugin_entities, $plugin_id);
|
||||
$plugins[$plugin_id] = $plugin;
|
||||
|
||||
// Add the relation in the reverse direction.
|
||||
$name = $this_col . '-' . $to_entity . '-' . $from_entity;
|
||||
$plugin_id = $parent . ':' . $name;
|
||||
$plugin = $parent_plugin;
|
||||
|
||||
$plugin['title'] = t('@from_entity from @to_entity (on @base_table.@relationship)', $replacements);
|
||||
$plugin['keyword'] = $to_entity;
|
||||
$plugin['context name'] = $name;
|
||||
$plugin['name'] = $plugin_id;
|
||||
$plugin['description'] = t('Builds a relationship from a @to_entity to a @from_entity using the @base_table.@relationship field.', $replacements);
|
||||
|
||||
$plugin['required context'] = new ctools_context_required($to_entity_info['label'], $to_entity);
|
||||
|
||||
$plugin_entities = array('to' => $from_entity_info, 'from' => $to_entity_info);
|
||||
$plugin_entities = array('to' => array($from_entity => $from_entity_info), 'from' => array($to_entity => $to_entity_info));
|
||||
drupal_alter('ctools_entity_context', $plugin, $plugin_entities, $plugin_id);
|
||||
$plugins[$plugin_id] = $plugin;
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
drupal_alter('ctools_entity_contexts', $plugins);
|
||||
return $plugins;
|
||||
}
|
||||
|
||||
function ctools_entity_from_schema_columns_filter($columns, $value) {
|
||||
foreach ($columns as $this_col => $that_col) {
|
||||
if ($value == $that_col) {
|
||||
return $this_col;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a new context based on an existing context.
|
||||
*/
|
||||
function ctools_entity_from_schema_context($context, $conf) {
|
||||
$plugin = $conf['name'];
|
||||
list($plugin, $plugin_name) = explode(':', $plugin);
|
||||
list($this_col, $from_entity, $to_entity) = explode('-', $plugin_name);
|
||||
// If unset it wants a generic, unfilled context, which is just NULL.
|
||||
$entity_info = entity_get_info($from_entity);
|
||||
if (empty($context->data) || !isset($context->data->{$entity_info['entity keys']['id']})) {
|
||||
return ctools_context_create_empty('entity:' . $to_entity, NULL);
|
||||
}
|
||||
|
||||
if (isset($context->data->{$entity_info['entity keys']['id']})) {
|
||||
// Load the entity.
|
||||
$id = $context->data->{$entity_info['entity keys']['id']};
|
||||
$entity = entity_load($from_entity, array($id));
|
||||
$entity = $entity[$id];
|
||||
if (isset($entity->$this_col)) {
|
||||
$to_entity_id = $entity->$this_col;
|
||||
|
||||
// Send it to ctools.
|
||||
return ctools_context_create('entity:' . $to_entity, $to_entity_id);
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Plugin to provide an relationship handler for term from node.
|
||||
*/
|
||||
|
||||
/**
|
||||
* 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 from node'),
|
||||
'keyword' => 'node_form',
|
||||
'description' => t('Adds node edit form from a node context.'),
|
||||
'required context' => new ctools_context_required(t('Node'), 'node'),
|
||||
'context' => 'ctools_node_edit_form_from_node_context',
|
||||
);
|
||||
|
||||
/**
|
||||
* Return a new context based on an existing context.
|
||||
*/
|
||||
function ctools_node_edit_form_from_node_context($context, $conf) {
|
||||
if (empty($context->data)) {
|
||||
return ctools_context_create_empty('node_edit_form', NULL);
|
||||
}
|
||||
|
||||
if (isset($context->data->nid)) {
|
||||
return ctools_context_create('node_edit_form', $context->data);
|
||||
}
|
||||
}
|
@@ -0,0 +1,60 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Plugin to provide an relationship handler for term from node.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Plugins are described by creating a $plugin array which will be used
|
||||
* by the system that includes this file.
|
||||
*/
|
||||
$plugin = array(
|
||||
'title' => t('Term from node'),
|
||||
'keyword' => 'term',
|
||||
'description' => t('Adds a taxonomy term from a node context; if multiple terms are selected, this will get the "first" term only.'),
|
||||
'required context' => new ctools_context_required(t('Node'), 'node'),
|
||||
'context' => 'ctools_term_from_node_context',
|
||||
'edit form' => 'ctools_term_from_node_settings_form',
|
||||
'defaults' => array('vid' => ''),
|
||||
);
|
||||
|
||||
/**
|
||||
* Return a new context based on an existing context.
|
||||
*/
|
||||
function ctools_term_from_node_context($context, $conf) {
|
||||
// If unset it wants a generic, unfilled context, which is just NULL.
|
||||
if (empty($context->data)) {
|
||||
return ctools_context_create_empty('entity:taxonomy_term', NULL);
|
||||
}
|
||||
|
||||
if (isset($context->data->taxonomy)) {
|
||||
foreach ($context->data->taxonomy as $term) {
|
||||
if ($term->vid == $conf['vid']) {
|
||||
return ctools_context_create('entity:taxonomy_term', $term);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Settings form for the relationship.
|
||||
*/
|
||||
function ctools_term_from_node_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'],
|
||||
'#prefix' => '<div class="clearfix">',
|
||||
'#suffix' => '</div>',
|
||||
);
|
||||
|
||||
return $form;
|
||||
}
|
@@ -0,0 +1,68 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file relationships/term_parent.inc
|
||||
* Plugin to provide an relationship handler for term parent.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Plugins are described by creating a $plugin array which will be used
|
||||
* by the system that includes this file.
|
||||
*/
|
||||
$plugin = array(
|
||||
'title' => t('Term parent'),
|
||||
'keyword' => 'parent_term',
|
||||
'description' => t('Adds a taxonomy term parent from a term context.'),
|
||||
'required context' => new ctools_context_required(t('Term'), 'entity:taxonomy_term'),
|
||||
'context' => 'ctools_term_parent_context',
|
||||
'edit form' => 'ctools_term_parent_settings_form',
|
||||
'defaults' => array('type' => 'top'),
|
||||
);
|
||||
|
||||
/**
|
||||
* Return a new context based on an existing context.
|
||||
*/
|
||||
function ctools_term_parent_context($context, $conf) {
|
||||
// If unset it wants a generic, unfilled context, which is just NULL.
|
||||
if (empty($context->data)) {
|
||||
return ctools_context_create_empty('entity:taxonomy_term');
|
||||
}
|
||||
|
||||
if (isset($context->data)) {
|
||||
$result = db_query('SELECT t1.* FROM {taxonomy_term_hierarchy} t1 INNER JOIN {taxonomy_term_hierarchy} t2 ON t1.tid = t2.parent WHERE t2.tid = :tid', array(':tid' => $context->data->tid))->fetchAssoc();
|
||||
|
||||
// If top level term, keep looking up until we see a top level.
|
||||
if ($conf['type'] == 'top') {
|
||||
// If looking for top level, and there are no parents at all, make sure
|
||||
// the current term is the 'top level'.
|
||||
if (empty($result)) {
|
||||
$result['tid'] = $context->data->tid;
|
||||
}
|
||||
while (!empty($result['parent'])) {
|
||||
$result = db_query('SELECT * FROM {taxonomy_term_hierarchy} WHERE tid = :tid', array(':tid' => $result['parent']))->fetchAssoc();
|
||||
}
|
||||
}
|
||||
|
||||
// Load the term.
|
||||
if ($result) {
|
||||
$term = taxonomy_term_load($result['tid']);
|
||||
return ctools_context_create('entity:taxonomy_term', $term);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Settings form for the relationship.
|
||||
*/
|
||||
function ctools_term_parent_settings_form($form, &$form_state) {
|
||||
$conf = $form_state['conf'];
|
||||
|
||||
$form['type'] = array(
|
||||
'#type' => 'select',
|
||||
'#title' => t('Relationship type'),
|
||||
'#options' => array('parent' => t('Immediate parent'), 'top' => t('Top level term')),
|
||||
'#default_value' => $conf['type'],
|
||||
);
|
||||
|
||||
return $form;
|
||||
}
|
@@ -0,0 +1,83 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Plugin to provide an relationship handler for all terms from node.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Plugins are described by creating a $plugin array which will be used
|
||||
* by the system that includes this file.
|
||||
*/
|
||||
$plugin = array(
|
||||
'title' => t('Multiple terms from node'),
|
||||
'keyword' => 'terms',
|
||||
'description' => t('Adds a taxonomy terms from a node context; if multiple terms are selected, they wil be concatenated.'),
|
||||
'required context' => new ctools_context_required(t('Node'), 'node'),
|
||||
'context' => 'ctools_terms_from_node_context',
|
||||
'edit form' => 'ctools_terms_from_node_settings_form',
|
||||
'defaults' => array('vocabulary' => array(), 'concatenator' => ','),
|
||||
);
|
||||
|
||||
/**
|
||||
* Return a new context based on an existing context.
|
||||
*/
|
||||
function ctools_terms_from_node_context($context, $conf) {
|
||||
// If unset it wants a generic, unfilled context, which is just NULL.
|
||||
if (empty($context->data)) {
|
||||
return ctools_context_create_empty('terms', NULL);
|
||||
}
|
||||
|
||||
// Collect all terms for the chosen vocabulary and concatenate them.
|
||||
$node = $context->data;
|
||||
$terms = array();
|
||||
|
||||
$fields = field_info_instances('node', $node->type);
|
||||
foreach ($fields as $name => $info) {
|
||||
$field_info = field_info_field($name);
|
||||
if ($field_info['type'] == 'taxonomy_term_reference' && (empty($conf['vocabulary']) || $conf['vocabulary'][$field_info['settings']['allowed_values'][0]['vocabulary']])) {
|
||||
$items = field_get_items('node', $node, $name);
|
||||
if (is_array($items)) {
|
||||
foreach ($items as $item) {
|
||||
$terms[] = $item['tid'];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!empty($terms)) {
|
||||
$all_terms = ctools_break_phrase(implode($conf['concatenator'], $terms));
|
||||
return ctools_context_create('terms', $all_terms);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Settings form for the relationship.
|
||||
*/
|
||||
function ctools_terms_from_node_settings_form($form, &$form_state) {
|
||||
$conf = $form_state['conf'];
|
||||
|
||||
$options = array();
|
||||
foreach (taxonomy_vocabulary_get_names() as $name => $vocabulary) {
|
||||
$options[$name] = $vocabulary->name;
|
||||
}
|
||||
$form['vocabulary'] = array(
|
||||
'#title' => t('Vocabulary'),
|
||||
'#type' => 'checkboxes',
|
||||
'#options' => $options,
|
||||
'#default_value' => $conf['vocabulary'],
|
||||
'#prefix' => '<div class="clearfix">',
|
||||
'#suffix' => '</div>',
|
||||
);
|
||||
$form['concatenator'] = array(
|
||||
'#title' => t('Concatenator'),
|
||||
'#type' => 'select',
|
||||
'#options' => array(',' => ', (AND)', '+' => '+ (OR)'),
|
||||
'#default_value' => $conf['concatenator'],
|
||||
'#prefix' => '<div class="clearfix">',
|
||||
'#suffix' => '</div>',
|
||||
'#description' => t("When the value from this context is passed on to a view as argument, the terms can be concatenated in the form of 1+2+3 (for OR) or 1,2,3 (for AND)."),
|
||||
);
|
||||
|
||||
return $form;
|
||||
}
|
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Plugin to provide an relationship handler for term from node.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Plugins are described by creating a $plugin array which will be used
|
||||
* by the system that includes this file.
|
||||
*/
|
||||
$plugin = array(
|
||||
'title' => t('User category edit form from user'),
|
||||
'keyword' => 'user_category_form',
|
||||
'description' => t('Adds user category edit form from a user context.'),
|
||||
'required context' => new ctools_context_required(t('User'), 'user'),
|
||||
'context' => 'ctools_user_category_edit_form_from_user_context',
|
||||
);
|
||||
|
||||
/**
|
||||
* Return a new context based on an existing context.
|
||||
*/
|
||||
function ctools_user_category_edit_form_from_user_context($context, $conf) {
|
||||
if (empty($context->data)) {
|
||||
return ctools_context_create_empty('user_edit_form', NULL);
|
||||
}
|
||||
|
||||
if (isset($context->data->user_category)) {
|
||||
return ctools_context_create('user_edit_form', $context->data, array('category' => $context->data->user_category));
|
||||
}
|
||||
}
|
@@ -0,0 +1,38 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Plugin to provide an relationship handler for node from user.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Plugins are described by creating a $plugin array which will be used
|
||||
* by the system that includes this file.
|
||||
*/
|
||||
$plugin = array(
|
||||
'title' => t('Node author'),
|
||||
'keyword' => 'user',
|
||||
'description' => t('Creates the author of a node as a user context.'),
|
||||
'required context' => new ctools_context_required(t('Node'), 'node'),
|
||||
'context' => 'ctools_user_from_node_context',
|
||||
'no ui' => TRUE,
|
||||
);
|
||||
|
||||
/**
|
||||
* Return a new context based on an existing context.
|
||||
*/
|
||||
function ctools_user_from_node_context($context, $conf) {
|
||||
// If unset it wants a generic, unfilled context, which is just NULL.
|
||||
if (empty($context->data) || !isset($context->data->uid)) {
|
||||
return ctools_context_create_empty('user', NULL);
|
||||
}
|
||||
|
||||
if (isset($context->data->uid)) {
|
||||
// Load the user that is the author of the node.
|
||||
$uid = $context->data->uid;
|
||||
$account = user_load($uid);
|
||||
|
||||
// Send it to ctools.
|
||||
return ctools_context_create('user', $account);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user