Last active
January 4, 2016 07:39
-
-
Save markisatacomputer/8589775 to your computer and use it in GitHub Desktop.
tag entities - vbo custom action
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
* Helper function | |
*/ | |
function entity_tag_action_format_tid_array($tids) { | |
$formatted = array(); | |
foreach ($tids as $tid) { | |
if (is_numeric($tid)) { | |
$formatted[] = array('tid' => $tid); | |
} else if (is_array($tid) && $tid['tid'] == 'autocreate') { | |
$formatted[] = array('tid' => entity_tag_action_autocreate($tid)); | |
} | |
} | |
return $formatted; | |
} | |
/* | |
* Helper function | |
*/ | |
function entity_tag_action_autocreate($trm) { | |
$term = new stdClass(); | |
$term->name = $trm['name']; | |
$term->vid = $trm['vid']; | |
$term->vocabulary_machine_name = $trm['vocabulary_machine_name']; | |
taxonomy_term_save($term); | |
return $term->tid; | |
} | |
/* | |
* Helper function | |
*/ | |
function entity_tag_action_compress_array($existing) { | |
$compressed = array(); | |
foreach ($existing as $e) { | |
$compressed[] = $e['tid']; | |
} | |
return $compressed; | |
} | |
/* | |
* Helper function | |
*/ | |
function entity_tag_action_merge($existing, $new) { | |
$existing = entity_tag_action_compress_array($existing); | |
$all = array_values(array_unique(array_merge($existing, $new))); | |
return $all; | |
} | |
/** | |
* Configuration form shown to the user before the action gets executed. | |
*/ | |
function entity_tag_action_form($context, &$form_state) { | |
$form = array('#parents' => array()); | |
// Get all our instance field info | |
$entity_type = $context['entity_type']; | |
$bundle = array_shift($context['view']->filter['type']->value); | |
$instances = field_info_instances($entity_type, $bundle); | |
// Load all the taxonomy term form elements from this entity type and bundle | |
foreach ($instances as $field_name => $instance) { | |
$field = field_info_field($field_name); | |
if ($field['type'] == 'taxonomy_term_reference') { | |
$l = isset($field['language']) ? $field['language'] : LANGUAGE_NONE; | |
$instance = field_info_instance($entity_type, $field_name, $bundle); | |
$form[$field_name] = field_default_form($entity_type, NULL, $field, $instance, $l, NULL, $form, $form_state); | |
if ($field['cardinality'] > 1) { | |
$form[$field_name]['#prefix'] = '<p class="caution">' . | |
t('CAUTION! This field only accepts a limited number of values - ' . $field['cardinality']. ' to be exact. You may be replacing old values with what you enter here.') | |
. '</p>'; | |
} else if ($field['cardinality'] == 1){ | |
$form[$field_name]['#prefix'] = '<p class="caution">' . | |
t('CAUTION! This field only accepts ONE value. If you enter something here, the old value will be replaced.') | |
. '</p>'; | |
} | |
} | |
} | |
return $form; | |
} | |
function entity_tag_action_submit(&$form, &$form_state) { | |
$tags = array(); | |
foreach ($form_state['values'] as $field_name => $terms) { | |
$l = isset($form[$field_name]['#language']) ? $form[$field_name]['#language'] : LANGUAGE_NONE; | |
$total = count($terms[$l]); | |
if (is_array($terms[$l])) { | |
foreach ($terms[$l] as $k => $trm) { | |
if (is_numeric($trm['tid'])) { | |
$tags[$field_name][] = $trm['tid']; | |
} else if ($trm['tid'] == 'autocreate') { | |
$tags[$field_name][] = $trm; | |
} | |
} | |
if (isset($tags[$field_name]) && is_array($tags[$field_name])) { | |
$tags[$field_name] = array_unique($tags[$field_name]); | |
} | |
} | |
} | |
return array( | |
'tags' => $tags, | |
); | |
} | |
function entity_tag_action(&$entity, $context) { | |
$l = entity_language($context['entity_type'], $entity) ? entity_language($context['entity_type'], $entity) : LANGUAGE_NONE; | |
foreach ($context['tags'] as $field_name => $tags) { | |
// What's the field's cardinality? | |
$field = field_info_field($field_name); | |
$cardinality = $field['cardinality']; | |
// how many terms exist already? | |
$existing = $entity->$field_name; | |
$current_total = count($existing[$l]); // one and zero are the same as far as we care | |
// Additive | |
if ($cardinality == -1) { | |
// If existing is empty let's provide an array to merge with | |
$add_to = isset($existing[$l]) ? $existing[$l] : array(); | |
$new = entity_tag_action_merge($add_to, $tags); | |
$new = entity_tag_action_format_tid_array($new); | |
// Simple replacement | |
} else if ($cardinality == 1) { | |
$new = entity_tag_action_format_tid_array($tags); | |
// Keep some existing | |
} else if ($cardinality >= $current_total) { | |
$all = entity_tag_action_merge($entity->$field_name[$l], $tags); | |
$keep = array_slice($all, -$cardinality); | |
$new = entity_tag_action_format_tid_array($keep); | |
} else if ($cardinality < $current_total) { | |
//this situation should be impossible | |
watchdog('entity_tag_action', t('Something real messed up just happened. I recommend you get down here right now.')); | |
$new = entity_tag_action_format_tid_array($tags); | |
} | |
$entity->$field_name = array($l => array_values($new)); | |
} | |
entity_save($context['entity_type'], $entity); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
* Implements hook_action_info(). | |
*/ | |
function entity_tag_action_info() { | |
module_load_include('inc', 'entity_tag', 'entity_tag.action'); | |
$actions['entity_tag_action'] = array( | |
'type' => 'entity', | |
'label' => t('Tag Images'), | |
'configurable' => FALSE, | |
'vbo_configurable' => TRUE, | |
'triggers' => array('any'), | |
); | |
return $actions; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment