|
@@ -358,8 +358,7 @@ function tripal_load_bundle_entity($values) {
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
- * Allows a module to write to the admin notification table
|
|
|
|
- * during the cron run.
|
|
|
|
|
|
+ * Allows a module to write to the admin notification table.
|
|
*
|
|
*
|
|
* @param $title
|
|
* @param $title
|
|
* A generic phrase indicating what the notification is for.
|
|
* A generic phrase indicating what the notification is for.
|
|
@@ -628,6 +627,8 @@ function tripal_get_content_type($bundle_name) {
|
|
*
|
|
*
|
|
* @param $bundle_name
|
|
* @param $bundle_name
|
|
* The name of the bundle to refresh (e.g. bio_data_4).
|
|
* The name of the bundle to refresh (e.g. bio_data_4).
|
|
|
|
+ * @param $term
|
|
|
|
+ * The term object for the bundle.
|
|
*
|
|
*
|
|
* @return
|
|
* @return
|
|
* The array of field instance names that were added.
|
|
* The array of field instance names that were added.
|
|
@@ -635,38 +636,34 @@ function tripal_get_content_type($bundle_name) {
|
|
* @ingroup tripal_entities_api
|
|
* @ingroup tripal_entities_api
|
|
*/
|
|
*/
|
|
function tripal_create_bundle_fields($bundle, $term) {
|
|
function tripal_create_bundle_fields($bundle, $term) {
|
|
-
|
|
|
|
$added = array();
|
|
$added = array();
|
|
|
|
|
|
// Allow modules to add fields to the new bundle.
|
|
// Allow modules to add fields to the new bundle.
|
|
$modules = module_implements('bundle_fields_info');
|
|
$modules = module_implements('bundle_fields_info');
|
|
- $info = array();
|
|
|
|
|
|
+ $field_info = array();
|
|
foreach ($modules as $module) {
|
|
foreach ($modules as $module) {
|
|
$function = $module . '_bundle_fields_info';
|
|
$function = $module . '_bundle_fields_info';
|
|
$temp = $function('TripalEntity', $bundle);
|
|
$temp = $function('TripalEntity', $bundle);
|
|
if (is_array($temp)) {
|
|
if (is_array($temp)) {
|
|
// TODO: it would be good to check this array to make sure it follows
|
|
// TODO: it would be good to check this array to make sure it follows
|
|
// protocol. It would help identify potential errors.
|
|
// protocol. It would help identify potential errors.
|
|
- $info = array_merge($info, $temp);
|
|
|
|
|
|
+ $field_info = array_merge($field_info, $temp);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
// Allow modules to alter which fields should be attached to content
|
|
// Allow modules to alter which fields should be attached to content
|
|
// types they create.
|
|
// types they create.
|
|
- drupal_alter('bundle_fields_info', $info, $bundle, $term);
|
|
|
|
|
|
+ drupal_alter('bundle_fields_info', $field_info, $bundle, $term);
|
|
|
|
|
|
// Iterate through all of the fields and create them.
|
|
// Iterate through all of the fields and create them.
|
|
- foreach ($info as $field_name => $details) {
|
|
|
|
|
|
+ foreach ($field_info as $field_name => $details) {
|
|
$field_type = $details['type'];
|
|
$field_type = $details['type'];
|
|
|
|
|
|
- // TODO: make sure the field term exits. If not then
|
|
|
|
- // skip it.
|
|
|
|
-
|
|
|
|
// If the field already exists then skip it.
|
|
// If the field already exists then skip it.
|
|
$field = field_info_field($details['field_name']);
|
|
$field = field_info_field($details['field_name']);
|
|
if ($field) {
|
|
if ($field) {
|
|
continue;
|
|
continue;
|
|
- }
|
|
|
|
|
|
+ }
|
|
|
|
|
|
// Create the field.
|
|
// Create the field.
|
|
$field = field_create_field($details);
|
|
$field = field_create_field($details);
|
|
@@ -678,35 +675,99 @@ function tripal_create_bundle_fields($bundle, $term) {
|
|
|
|
|
|
// Allow modules to add instances to the new bundle.
|
|
// Allow modules to add instances to the new bundle.
|
|
$modules = module_implements('bundle_instances_info');
|
|
$modules = module_implements('bundle_instances_info');
|
|
- $info = array();
|
|
|
|
|
|
+ $instance_info = array();
|
|
foreach ($modules as $module) {
|
|
foreach ($modules as $module) {
|
|
$function = $module . '_bundle_instances_info';
|
|
$function = $module . '_bundle_instances_info';
|
|
$temp = $function('TripalEntity', $bundle);
|
|
$temp = $function('TripalEntity', $bundle);
|
|
if (is_array($temp)) {
|
|
if (is_array($temp)) {
|
|
// TODO: it would be good to check this array to make sure it follows
|
|
// TODO: it would be good to check this array to make sure it follows
|
|
// protocol. It would help identify potential errors.
|
|
// protocol. It would help identify potential errors.
|
|
- $info = array_merge($info, $temp);
|
|
|
|
|
|
+ $instance_info = array_merge($instance_info, $temp);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
// Allow modules to alter which fields should be attached to content
|
|
// Allow modules to alter which fields should be attached to content
|
|
// types they create.
|
|
// types they create.
|
|
- drupal_alter('bundle_instances_info', $info, $bundle, $term);
|
|
|
|
-
|
|
|
|
|
|
+ drupal_alter('bundle_instances_info', $instance_info, $bundle, $term);
|
|
|
|
+
|
|
|
|
+ // Get the list of existing instances
|
|
|
|
+ $existing_instances = field_info_instances('TripalEntity', $bundle->name);
|
|
|
|
+
|
|
// Iterate through all of the field instances and create them.
|
|
// Iterate through all of the field instances and create them.
|
|
- foreach ($info as $field_name => $details) {
|
|
|
|
|
|
+ foreach ($instance_info as $instance_name => $details) {
|
|
|
|
+
|
|
|
|
+ // Make sure the instance has a term. If not, report it and skip the field.
|
|
|
|
+ if (!array_key_exists('term_vocabulary', $details['settings'])) {
|
|
|
|
+ tripal_report_error('tripal_fields', TRIPAL_WARNING,
|
|
|
|
+ 'The field instance, !field, is missing the "term_vocabulary" setting. The field instance cannot be added. Please check the field settings.',
|
|
|
|
+ ['!field' => $instance_name], ['drupal_set_message' => TRUE]);
|
|
|
|
+ continue;
|
|
|
|
+ }
|
|
|
|
+ if (!array_key_exists('term_accession', $details['settings'])) {
|
|
|
|
+ tripal_report_error('tripal_fields', TRIPAL_WARNING,
|
|
|
|
+ 'The field instance, !field, is missing the "term_accession" setting. The field instance cannot be added. Please check the field settings.',
|
|
|
|
+ ['!field' => $instance_name], ['drupal_set_message' => TRUE]);
|
|
|
|
+ continue;}
|
|
|
|
+
|
|
|
|
+ // Make sure the term exists. If not, skip the field instance and
|
|
|
|
+ // report an error.
|
|
|
|
+ $field_term_id = $details['settings']['term_vocabulary'] . ':' . $details['settings']['term_accession'];
|
|
|
|
+ $field_term = tripal_get_term_details($details['settings']['term_vocabulary'], $details['settings']['term_accession']);
|
|
|
|
+ if (!$field_term) {
|
|
|
|
+ tripal_report_error('tripal_fields', TRIPAL_WARNING,
|
|
|
|
+ 'The term, !term, for the field, !field, does not exist in the database. The ' .
|
|
|
|
+ 'field instance cannot be added. Please make sure the term is correct and add it if necessary.',
|
|
|
|
+ ['!term' => $field_term_id,
|
|
|
|
+ '!field' => $instance_name],
|
|
|
|
+ ['drupal_set_message' => TRUE]);
|
|
|
|
+ continue;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // Make sure the term is not used for any other existing field instance.
|
|
|
|
+ $skip = TRUE;
|
|
|
|
+ foreach ($existing_instances as $existing_name => $existing_instance) {
|
|
|
|
+ // If this instance term is the same as this exsiting term and the
|
|
|
|
+ // instance name is not the same then we have a problem.
|
|
|
|
+ $existing_term_id = $existing_instance['settings']['term_vocabulary'] . ':' . $existing_instance['settings']['term_accession'];
|
|
|
|
+ $existing_field = field_info_field($existing_name);
|
|
|
|
+ if ($existing_term_id == $field_term_id and $instance_name != $existing_name) {
|
|
|
|
+ tripal_report_error('tripal_fields', TRIPAL_WARNING,
|
|
|
|
+ 'The field, !field, uses a term, !term, that is already in use on this content type. The ' .
|
|
|
|
+ 'field instance cannot be added.',
|
|
|
|
+ ['!term' => $existing_term_id,
|
|
|
|
+ '!field' => $instance_name],
|
|
|
|
+ ['drupal_set_message' => TRUE]);
|
|
|
|
+ $skip = TRUE;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // If the instance term is the same as this exsting term but the storage
|
|
|
|
+ // types are different then we have a problem.
|
|
|
|
+ $existing_storage = $existing_field['storage']['type'];
|
|
|
|
+ $this_field_storage = $field_info[$details['field_name']]['storage']['type'];
|
|
|
|
+ if ($existing_term_id == $field_term_id and $existing_storage != $this_field_storage) {
|
|
|
|
+ tripal_report_error('tripal_fields', TRIPAL_WARNING,
|
|
|
|
+ 'The field, !field, provided by the storage type, !type, uses a term, !term, that is already in use on this content type and provided by another storage backend. The ' .
|
|
|
|
+ 'field instance cannot be added. Perhaps, consider a different term and adjust the data in the database.',
|
|
|
|
+ ['!term' => $existing_term_id,
|
|
|
|
+ '!type' => $this_field_storage,
|
|
|
|
+ '!field' => $instance_name],
|
|
|
|
+ ['drupal_set_message' => TRUE]);
|
|
|
|
+ $skip = TRUE;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ if ($skip) {
|
|
|
|
+ continue;
|
|
|
|
+ }
|
|
|
|
|
|
// If the field is already attached to this bundle then skip it.
|
|
// If the field is already attached to this bundle then skip it.
|
|
- $field = field_info_field($details['field_name']);
|
|
|
|
- if ($field and array_key_exists('bundles', $field) and
|
|
|
|
- array_key_exists('TripalEntity', $field['bundles']) and
|
|
|
|
- in_array($bundle->name, $field['bundles']['TripalEntity'])) {
|
|
|
|
|
|
+ if (array_key_exists($instance_name, $existing_instances)) {
|
|
continue;
|
|
continue;
|
|
}
|
|
}
|
|
|
|
|
|
// Create the field instance.
|
|
// Create the field instance.
|
|
$instance = field_create_instance($details);
|
|
$instance = field_create_instance($details);
|
|
- $added[] = $field_name;
|
|
|
|
|
|
+ $existing_instances[$instance_name] = $instance;
|
|
|
|
+ $added[] = $instance_name;
|
|
}
|
|
}
|
|
return $added;
|
|
return $added;
|
|
}
|
|
}
|
|
@@ -1008,14 +1069,14 @@ function tripal_get_default_title_format($bundle) {
|
|
/**
|
|
/**
|
|
* Returns an array of tokens based on Tripal Entity Fields.
|
|
* Returns an array of tokens based on Tripal Entity Fields.
|
|
*
|
|
*
|
|
- * @param TripalBundle $entity
|
|
|
|
|
|
+ * @param TripalBundle $bundle
|
|
* The bundle entity for which you want tokens.
|
|
* The bundle entity for which you want tokens.
|
|
* @return
|
|
* @return
|
|
* An array of tokens where the key is the machine_name of the token.
|
|
* An array of tokens where the key is the machine_name of the token.
|
|
*
|
|
*
|
|
* @ingroup tripal_entities_api
|
|
* @ingroup tripal_entities_api
|
|
*/
|
|
*/
|
|
-function tripal_get_entity_tokens($entity, $options = array()) {
|
|
|
|
|
|
+function tripal_get_entity_tokens($bundle, $options = array()) {
|
|
$tokens = array();
|
|
$tokens = array();
|
|
|
|
|
|
// Set default options.
|
|
// Set default options.
|
|
@@ -1024,51 +1085,132 @@ function tripal_get_entity_tokens($entity, $options = array()) {
|
|
|
|
|
|
if ($options['include id']) {
|
|
if ($options['include id']) {
|
|
$token = '[TripalBundle__bundle_id]';
|
|
$token = '[TripalBundle__bundle_id]';
|
|
- $tokens[$token] = array(
|
|
|
|
|
|
+ $tokens[$token] = [
|
|
'label' => 'Bundle ID',
|
|
'label' => 'Bundle ID',
|
|
'description' => 'The unique identifier for this Tripal Content Type.',
|
|
'description' => 'The unique identifier for this Tripal Content Type.',
|
|
'token' => $token,
|
|
'token' => $token,
|
|
'field_name' => NULL,
|
|
'field_name' => NULL,
|
|
'required' => TRUE
|
|
'required' => TRUE
|
|
- );
|
|
|
|
|
|
+ ];
|
|
|
|
|
|
$token = '[TripalEntity__entity_id]';
|
|
$token = '[TripalEntity__entity_id]';
|
|
- $tokens[$token] = array(
|
|
|
|
|
|
+ $tokens[$token] = [
|
|
'label' => 'Content/Entity ID',
|
|
'label' => 'Content/Entity ID',
|
|
'description' => 'The unique identifier for an individual piece of Tripal Content.',
|
|
'description' => 'The unique identifier for an individual piece of Tripal Content.',
|
|
'token' => $token,
|
|
'token' => $token,
|
|
'field_name' => NULL,
|
|
'field_name' => NULL,
|
|
'required' => TRUE
|
|
'required' => TRUE
|
|
- );
|
|
|
|
|
|
+ ];
|
|
}
|
|
}
|
|
|
|
|
|
- $fields = field_info_instances('TripalEntity', $entity->name);
|
|
|
|
- foreach ($fields as $f) {
|
|
|
|
-
|
|
|
|
- // Build the token from the field information.
|
|
|
|
- $token = '[' . $f['field_name'] . ']';
|
|
|
|
- $current_token = array(
|
|
|
|
- 'label' => $f['label'],
|
|
|
|
- 'description' => $f['description'],
|
|
|
|
- 'token' => $token,
|
|
|
|
- 'field_name' => $f['field_name'],
|
|
|
|
- 'required' => $f['required']
|
|
|
|
- );
|
|
|
|
-
|
|
|
|
- // If the required only option is set then we only want to add
|
|
|
|
- // required fields to the token list.
|
|
|
|
- if ($options['required only'] AND $current_token['required']) {
|
|
|
|
- $tokens[$token] = $current_token;
|
|
|
|
|
|
+ $instances = field_info_instances('TripalEntity', $bundle->name);
|
|
|
|
+ foreach ($instances as $instance_name => $instance) {
|
|
|
|
+
|
|
|
|
+ if (!$instance['required'] and $options['required only']) {
|
|
|
|
+ continue;
|
|
}
|
|
}
|
|
- // If the required only option is not set then add everything.
|
|
|
|
- elseif (!$options['required only']) {
|
|
|
|
- $tokens[$token] = $current_token;
|
|
|
|
|
|
+
|
|
|
|
+ $use_field = FALSE;
|
|
|
|
+
|
|
|
|
+ // Iterate through the TripalEntity fields and see if they have
|
|
|
|
+ // sub-elements, if so, add those as tokens too.
|
|
|
|
+ $field_name = $instance['field_name'];
|
|
|
|
+ if ($instance['entity_type'] == 'TripalEntity') {
|
|
|
|
+ if (tripal_load_include_field_class($field_name)) {
|
|
|
|
+ $field = field_info_field($field_name);
|
|
|
|
+ $field_obj = new $field_name($field, $instance);
|
|
|
|
+ $element_info = $field_obj->elementInfo();
|
|
|
|
+ $term_id = $instance['settings']['term_vocabulary'] . ':' . $instance['settings']['term_accession'];
|
|
|
|
+ if ($element_info and
|
|
|
|
+ array_key_exists($term_id, $element_info) and
|
|
|
|
+ array_key_exists('elements', $element_info[$term_id]) and count($element_info[$term_id]['elements']) > 0) {
|
|
|
|
+ $elements = $element_info[$term_id]['elements'];
|
|
|
|
+ _tripal_get_entity_tokens_for_elements($instance, $field_name, $elements, $tokens, $options);
|
|
|
|
+ }
|
|
|
|
+ else {
|
|
|
|
+ $use_field = TRUE;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ else {
|
|
|
|
+ $use_field = TRUE;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ else {
|
|
|
|
+ $use_field = TRUE;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // If we have no elements to add then just add the field as is.
|
|
|
|
+ if ($use_field) {
|
|
|
|
+ // Build the token from the field information.
|
|
|
|
+ $token = '[' . $instance['field_name'] . ']';
|
|
|
|
+ $tokens[$token] = [
|
|
|
|
+ 'label' => $instance['label'],
|
|
|
|
+ 'description' => $instance['description'],
|
|
|
|
+ 'token' => $token,
|
|
|
|
+ 'field_name' => $instance['field_name'],
|
|
|
|
+ 'required' => $instance['required']
|
|
|
|
+ ];
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
return $tokens;
|
|
return $tokens;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+/**
|
|
|
|
+ * A recursive helper function to get tokens for element sub fields.
|
|
|
|
+ *
|
|
|
|
+ * @param $instance
|
|
|
|
+ * A original field instance object.
|
|
|
|
+ * @param $parent
|
|
|
|
+ * The name of the parent. The first time this is called outside of
|
|
|
|
+ * recursion this should be the field name.
|
|
|
|
+ * @param $elements
|
|
|
|
+ * The array of elements to process.
|
|
|
|
+ * @param $tokens
|
|
|
|
+ * The array of tokens to be added to.
|
|
|
|
+ */
|
|
|
|
+function _tripal_get_entity_tokens_for_elements($instance, $parent, $elements, &$tokens, $options) {
|
|
|
|
+
|
|
|
|
+ // Iterate through all of the elements and add tokens for each one.
|
|
|
|
+ foreach ($elements as $child_term_id => $details) {
|
|
|
|
+
|
|
|
|
+ // We don't need to add the entity element.
|
|
|
|
+ if ($child_term_id == 'entity') {
|
|
|
|
+ continue;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // Skip elements that aren't required.
|
|
|
|
+ $required = array_key_exists('required', $details) ? $details['required'] : FALSE;
|
|
|
|
+ if (!$required and $options['required only']) {
|
|
|
|
+ continue;
|
|
|
|
+ }
|
|
|
|
+ $token = '[' . $parent . ',' . $child_term_id . ']';
|
|
|
|
+ $label = $child_term_id;
|
|
|
|
+ if (array_key_exists('name', $details)) {
|
|
|
|
+ $label = $details['name'];
|
|
|
|
+ }
|
|
|
|
+ elseif (preg_match('/:/', $child_term_id)) {
|
|
|
|
+ list($vocabulary, $accession) = explode(':', $child_term_id);
|
|
|
|
+ $term = tripal_get_term_details($vocabulary, $accession);
|
|
|
|
+ $label = $term['name'];
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // Add the token!
|
|
|
|
+ $tokens[$token] = [
|
|
|
|
+ 'label' => $label,
|
|
|
|
+ 'description' => array_key_exists('description', $details) ? $details['description'] : '',
|
|
|
|
+ 'token' => $token,
|
|
|
|
+ 'field_name' => $instance['field_name'],
|
|
|
|
+ 'required' => $required
|
|
|
|
+ ];
|
|
|
|
+
|
|
|
|
+ // Recurse to include sub elements
|
|
|
|
+ if (array_key_exists('elements', $details)) {
|
|
|
|
+ _tripal_get_entity_tokens_for_elements($instance, $parent . ',' . $child_term_id,
|
|
|
|
+ $details['elements'], $tokens, $options);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+}
|
|
/**
|
|
/**
|
|
* Replace all Tripal Tokens in a given string.
|
|
* Replace all Tripal Tokens in a given string.
|
|
*
|
|
*
|
|
@@ -1089,8 +1231,8 @@ function tripal_get_entity_tokens($entity, $options = array()) {
|
|
*/
|
|
*/
|
|
function tripal_replace_entity_tokens($string, &$entity, $bundle_entity = NULL) {
|
|
function tripal_replace_entity_tokens($string, &$entity, $bundle_entity = NULL) {
|
|
// Determine which tokens were used in the format string
|
|
// Determine which tokens were used in the format string
|
|
- $used_tokens = array();
|
|
|
|
- if (preg_match_all('/\[\w+\]/', $string, $matches)) {
|
|
|
|
|
|
+ $used_tokens = [];
|
|
|
|
+ if (preg_match_all('/\[.*?\]/', $string, $matches)) {
|
|
$used_tokens = $matches[0];
|
|
$used_tokens = $matches[0];
|
|
}
|
|
}
|
|
|
|
|
|
@@ -1105,10 +1247,13 @@ function tripal_replace_entity_tokens($string, &$entity, $bundle_entity = NULL)
|
|
// all synced entities causes extreme slowness, so we'll only attach
|
|
// all synced entities causes extreme slowness, so we'll only attach
|
|
// the necessary fields for replacing tokens.
|
|
// the necessary fields for replacing tokens.
|
|
$attach_fields = array();
|
|
$attach_fields = array();
|
|
- foreach($used_tokens as $token) {
|
|
|
|
- $field_name = str_replace(array('.','[',']'), array('__','',''), $token);
|
|
|
|
|
|
|
|
- if (!property_exists($entity, $field_name)) {
|
|
|
|
|
|
+ foreach($used_tokens as $token) {
|
|
|
|
+ $token = preg_replace('/[\[\]]/', '', $token);
|
|
|
|
+ $elements = explode(',', $token);
|
|
|
|
+ $field_name = array_shift($elements);
|
|
|
|
+ //$field_name = str_replace(array('.','[',']'), array('__','',''), $field_name);
|
|
|
|
+ if (!property_exists($entity, $field_name) or empty($entity->{$field_name})) {
|
|
$field = field_info_field($field_name);
|
|
$field = field_info_field($field_name);
|
|
$storage = $field['storage'];
|
|
$storage = $field['storage'];
|
|
$attach_fields[$storage['type']]['storage'] = $storage;
|
|
$attach_fields[$storage['type']]['storage'] = $storage;
|
|
@@ -1130,20 +1275,28 @@ function tripal_replace_entity_tokens($string, &$entity, $bundle_entity = NULL)
|
|
module_invoke($storage['module'], 'field_storage_load', 'TripalEntity',
|
|
module_invoke($storage['module'], 'field_storage_load', 'TripalEntity',
|
|
$entities, FIELD_LOAD_CURRENT, $field_ids, array());
|
|
$entities, FIELD_LOAD_CURRENT, $field_ids, array());
|
|
}
|
|
}
|
|
-
|
|
|
|
|
|
+
|
|
// Now that all necessary fields are attached process the tokens.
|
|
// Now that all necessary fields are attached process the tokens.
|
|
foreach($used_tokens as $token) {
|
|
foreach($used_tokens as $token) {
|
|
- $field_name = str_replace(array('.','[',']'), array('__','',''), $token);
|
|
|
|
|
|
+ $token = preg_replace('/[\[\]]/', '', $token);
|
|
|
|
+ $elements = explode(',', $token);
|
|
|
|
+ $field_name = array_shift($elements);
|
|
$value = '';
|
|
$value = '';
|
|
-
|
|
|
|
|
|
+
|
|
if (property_exists($entity, $field_name)) {
|
|
if (property_exists($entity, $field_name)) {
|
|
|
|
+ $value = '';
|
|
// Note: there is a memory leak in field_get_items() so we can't use it
|
|
// Note: there is a memory leak in field_get_items() so we can't use it
|
|
// here or bulk publishing will slowly erode memory.
|
|
// here or bulk publishing will slowly erode memory.
|
|
- //$field_value = field_get_items('TripalEntity', $entity, $field_name);
|
|
|
|
- if (array_key_exists(0, $entity->{$field_name}['und'])) {
|
|
|
|
|
|
+ // $field_value = field_get_items('TripalEntity', $entity, $field_name);
|
|
|
|
+ if (array_key_exists('und', $entity->{$field_name}) and
|
|
|
|
+ array_key_exists(0, $entity->{$field_name}['und'])) {
|
|
$value = $entity->{$field_name}['und'][0]['value'];
|
|
$value = $entity->{$field_name}['und'][0]['value'];
|
|
- }
|
|
|
|
- // TODO: deal with the value when it is not a scalar.
|
|
|
|
|
|
+ // If the value is an array it means we have sub elements and we can
|
|
|
|
+ // descend through the array to look for matching value.
|
|
|
|
+ if (is_array($value) and count($elements) > 0) {
|
|
|
|
+ $value = _tripal_replace_entity_tokens_for_elements($elements, $value);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
}
|
|
}
|
|
// The TripalBundle__bundle_id is a special token for substituting the
|
|
// The TripalBundle__bundle_id is a special token for substituting the
|
|
// bundle id.
|
|
// bundle id.
|
|
@@ -1164,16 +1317,33 @@ function tripal_replace_entity_tokens($string, &$entity, $bundle_entity = NULL)
|
|
|
|
|
|
// We can't support tokens that have multiple elements (i.e. in an array).
|
|
// We can't support tokens that have multiple elements (i.e. in an array).
|
|
if (is_array($value)) {
|
|
if (is_array($value)) {
|
|
- $string = str_replace($token, '', $string);
|
|
|
|
|
|
+ $string = str_replace('[' . $token . ']', '', $string);
|
|
}
|
|
}
|
|
else {
|
|
else {
|
|
- $string = str_replace($token, $value, $string);
|
|
|
|
|
|
+ $string = str_replace('[' . $token . ']', $value, $string);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
return $string;
|
|
return $string;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+/**
|
|
|
|
+ * A helper function for tripal_replace_entity_tokens to get token values.
|
|
|
|
+ *
|
|
|
|
+ * This helper function is used when the tokens are from subelements.
|
|
|
|
+ * @param $entity
|
|
|
|
+ */
|
|
|
|
+function _tripal_replace_entity_tokens_for_elements($elements, $values) {
|
|
|
|
+ $term_id = array_shift($elements);
|
|
|
|
+ $value = $values[$term_id];
|
|
|
|
+ if (count($elements) == 0) {
|
|
|
|
+ return $value;
|
|
|
|
+ }
|
|
|
|
+ else {
|
|
|
|
+ _tripal_replace_entity_tokens_for_elements($elements, $value);
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
/**
|
|
/**
|
|
* Formats the tokens for display.
|
|
* Formats the tokens for display.
|
|
*
|
|
*
|