interdiff impossible; taking evasive action reverted: --- b/conditional_fields.features.inc +++ a/includes/conditional_fields.features.inc @@ -1,8 +1,7 @@ $dependees) { foreach ($dependees as $dependee) { + $code[] = " array("; + $code[] = " 'entity' => '" . $entity . "',"; + $code[] = " 'bundle' => '" . $bundle . "',"; + $code[] = " 'dependent' => '" . $dependent_field_name . "',"; + $code[] = " 'dependee' => '" . $dependee['dependee'] . "',"; + $code[] = " 'options' => " . features_var_export($dependee['options'], ' ') . ','; + $code[] = ' ),'; - $dependee = array( - 'entity' => $entity, - 'bundle' => $bundle, - 'dependent' => $dependent_field_name, - ) + $dependee; - - /* @see hook_conditional_fields_features_export_render_alter() */ - drupal_alter(__FUNCTION__, $dependee, $module_name); - - $code[] = ' ' . features_var_export($dependee, ' ') . ','; } } @@ -98,10 +94,7 @@ $dependencies = conditional_fields_load_dependencies(); $instances = field_info_instances(); foreach ($defaults as $item) { + foreach($item as $field) { - foreach ($item as $field) { - /* @see hook_conditional_fields_features_rebuild_alter() */ - drupal_alter(__FUNCTION__, $field, $module); - if (isset($dependencies[$field['entity']][$field['bundle']])) { foreach ($dependencies[$field['entity']][$field['bundle']] as $dependancy_type => $field_dep_settings) { if ($dependancy_type == 'dependees' && !empty($field_dep_settings)) { @@ -129,86 +122,5 @@ * Implements hook_features_revert(). */ function conditional_fields_features_revert($module) { + return conditional_fields_features_rebuild($module); - conditional_fields_features_rebuild($module); -} - -/** - * Implements hook_conditional_fields_features_export_render_alter(). - * - * @internal - */ -function conditional_fields_conditional_fields_features_export_render_alter(array &$dependee, $module_name) { - conditional_fields_features_entity_reference_process($dependee, 'entity_get_uuid_by_id'); -} - -/** - * Implements hook_conditional_fields_features_rebuild_alter(). - * - * @internal - */ -function conditional_fields_conditional_fields_features_rebuild_alter(array &$dependee, $module_name) { - conditional_fields_features_entity_reference_process($dependee, 'entity_get_id_by_uuid'); -} - -/** - * Allows exporting UUIDs of referenced entities instead of their IDs. - * - * @param array $dependee - * A dependee to process. - * @param string $exchanger - * A callback function to swap ID with UUID and vice versa. - * - * @internal - */ -function conditional_fields_features_entity_reference_process(array &$dependee, $exchanger) { - static $field_info = array(); - - if (function_exists($exchanger)) { - $field_parents_callback = function ($i) { - return array('value', $i, 'target_id'); - }; - - // Check for the entity reference field by specific parents. - if (NULL !== drupal_array_get_nested_value($dependee['options'], $field_parents_callback(0))) { - if (!isset($field_info[$dependee['dependee']])) { - $field_info[$dependee['dependee']] = field_info_field($dependee['dependee']); - } - - // Distinguish the type of widget by the parents in "value_form". - foreach (array( - // Hierarchical select widget. - function ($i) { - return array('value_form', 'hierarchical_select', 'selects', $i); - }, - // Default widgets. - function ($i) { - return array('value_form', $i, 'target_id'); - }, - ) as $widget_parents_callback) { - if (NULL !== drupal_array_get_nested_value($dependee['options'], $widget_parents_callback(0))) { - $ids_list = array(); - - foreach ($dependee['options']['value'] as $i => $option) { - $ids_list[$i] = $option['target_id']; - } - - $ids = $exchanger($field_info[$dependee['dependee']]['settings']['target_type'], $ids_list); - - // Need to iterate over the initial list to preserve ordering of - // values. - foreach ($ids_list as $i => $id) { - if (isset($ids[$id])) { - // Set value for the field and its form. - foreach (array($field_parents_callback($i), $widget_parents_callback($i)) as $parents) { - drupal_array_set_nested_value($dependee['options'], $parents, $ids[$id]); - } - } - } - - // A single field cannot have more than one widget so stop here. - break; - } - } - } - } } unchanged: --- /dev/null +++ b/conditional_fields.features.inc @@ -0,0 +1,218 @@ + $data) { + foreach ($data as $dependency => $info) { + $option = $type . ':' . $dependency; + if (!isset($options[$dependency])) { + $options[$option] = $entity_info[$type]['bundles'][$dependency]['label'] . ' (' . $type . ')'; + } + } + } + return $options; +} + +/** + * Implements hook_features_export(). + */ +function conditional_fields_features_export($data, &$export, $module_name = '') { + $export['dependencies']['conditional_fields'] = 'conditional_fields'; + + foreach ($data as $entity) { + $export['features']['conditional_fields'][$entity] = $entity; + } +} + +/** + * Implements hook_features_export_render(). + */ +function conditional_fields_features_export_render($module_name, $data, $export = NULL) { + $dependencies = conditional_fields_load_dependencies(); + if (empty($dependencies)) { + return array(); + } + + $code = array(); + foreach ($data as $option) { + $tokens = explode(':', $option); + $entity = $tokens[0]; + $bundle = $tokens[1]; + + if (isset($dependencies[$entity]) && isset($dependencies[$entity][$bundle])) { + $info = $dependencies[$entity][$bundle]; + + // Ensure that dependencies are always exported in the same order to + // avoid fake overrides. + ksort($info['dependents']); + + $code[] = ''; + $code[] = ' $items["' . $option . '"] = array('; + foreach ($info['dependents'] as $dependent_field_name => $dependees) { + foreach ($dependees as $dependee) { + $dependee = array( + 'entity' => $entity, + 'bundle' => $bundle, + 'dependent' => $dependent_field_name, + ) + $dependee; + + /* @see hook_conditional_fields_features_export_render_alter() */ + drupal_alter(__FUNCTION__, $dependee, $module_name); + + $code[] = ' ' . features_var_export($dependee, ' ') . ','; + } + } + + $code[] = ' );'; + } + } + if (empty($code)) { + return array(); + } + + array_unshift($code, ' $items = array();'); + $code[] = ''; + $code[] = ' return $items;'; + $output = implode("\n", $code); + + return array('conditional_fields_default_fields' => $output); +} + +/** + * Implements hook_features_rebuild(). + */ +function conditional_fields_features_rebuild($module) { + $defaults = features_get_default('conditional_fields', $module); + + if (!empty($defaults)) { + $dependencies = conditional_fields_load_dependencies(); + $instances = field_info_instances(); + foreach ($defaults as $item) { + foreach ($item as $field) { + /* @see hook_conditional_fields_features_rebuild_alter() */ + drupal_alter(__FUNCTION__, $field, $module); + + if (isset($dependencies[$field['entity']][$field['bundle']])) { + foreach ($dependencies[$field['entity']][$field['bundle']] as $dependancy_type => $field_dep_settings) { + if ($dependancy_type == 'dependees' && !empty($field_dep_settings)) { + foreach ($field_dep_settings as $dep_setting) { + foreach ($dep_setting as $id => $dep_settings) { + $deps_to_remove[$id] = $id; + } + } + } + } + if (!empty($deps_to_remove)) { + conditional_fields_dependency_delete($deps_to_remove); + } + } + $entity_instances = $instances[$field['entity']][$field['bundle']]; + $dependee_id = $entity_instances[$field['dependee']]['id']; + $dependent_id = $entity_instances[$field['dependent']]['id']; + conditional_fields_dependency_insert($dependee_id, $dependent_id, $field['options']); + } + } + } +} + +/** + * Implements hook_features_revert(). + */ +function conditional_fields_features_revert($module) { + conditional_fields_features_rebuild($module); +} + +/** + * Implements hook_conditional_fields_features_export_render_alter(). + * + * @internal + */ +function conditional_fields_conditional_fields_features_export_render_alter(array &$dependee, $module_name) { + conditional_fields_features_entity_reference_process($dependee, 'entity_get_uuid_by_id'); +} + +/** + * Implements hook_conditional_fields_features_rebuild_alter(). + * + * @internal + */ +function conditional_fields_conditional_fields_features_rebuild_alter(array &$dependee, $module_name) { + conditional_fields_features_entity_reference_process($dependee, 'entity_get_id_by_uuid'); +} + +/** + * Allows exporting UUIDs of referenced entities instead of their IDs. + * + * @param array $dependee + * A dependee to process. + * @param string $exchanger + * A callback function to swap ID with UUID and vice versa. + * + * @internal + */ +function conditional_fields_features_entity_reference_process(array &$dependee, $exchanger) { + static $field_info = array(); + + if (function_exists($exchanger)) { + $field_parents_callback = function ($i) { + return array('value', $i, 'target_id'); + }; + + // Check for the entity reference field by specific parents. + if (NULL !== drupal_array_get_nested_value($dependee['options'], $field_parents_callback(0))) { + if (!isset($field_info[$dependee['dependee']])) { + $field_info[$dependee['dependee']] = field_info_field($dependee['dependee']); + } + + // Distinguish the type of widget by the parents in "value_form". + foreach (array( + // Hierarchical select widget. + function ($i) { + return array('value_form', 'hierarchical_select', 'selects', $i); + }, + // Default widgets. + function ($i) { + return array('value_form', $i, 'target_id'); + }, + // The "options_select" widget. + function ($i) { + return array('value_form'); + }, + ) as $widget_parents_callback) { + if (NULL !== drupal_array_get_nested_value($dependee['options'], $widget_parents_callback(0))) { + $ids_list = array(); + + foreach ($dependee['options']['value'] as $i => $option) { + $ids_list[$i] = $option['target_id']; + } + + $ids = $exchanger($field_info[$dependee['dependee']]['settings']['target_type'], $ids_list); + + // Need to iterate over the initial list to preserve ordering of + // values. + foreach ($ids_list as $i => $id) { + if (isset($ids[$id])) { + // Set value for the field and its form. + foreach (array($field_parents_callback($i), $widget_parents_callback($i)) as $parents) { + drupal_array_set_nested_value($dependee['options'], $parents, $ids[$id]); + } + } + } + + // A single field cannot have more than one widget so stop here. + break; + } + } + } + } +} diff -u b/src/Tests/EntityReferenceFeaturesUuidExportImport/AutocompleteWidgetTest.php b/src/Tests/EntityReferenceFeaturesUuidExportImport/AutocompleteWidgetTest.php --- b/src/Tests/EntityReferenceFeaturesUuidExportImport/AutocompleteWidgetTest.php +++ b/src/Tests/EntityReferenceFeaturesUuidExportImport/AutocompleteWidgetTest.php @@ -16,12 +16,6 @@ const ENTITY_TYPE = 'node'; /** - * The list of modules to enable before the test. - * - * @var string[] - */ - protected $modules = array(); - /** * An associative array of instances of fields. * * @var array[] @@ -68,16 +62,38 @@ } /** + * Returns definition of the field widget to test export of. + * + * @return array + * Definition of the widget. + */ + protected static function getWidget() { + return array( + 'type' => 'entityreference_autocomplete', + 'active' => TRUE, + 'module' => 'entityreference', + 'settings' => array( + 'match_operator' => 'CONTAINS', + 'size' => 60, + 'path' => '', + ), + ); + } + + /** * {@inheritdoc} */ protected function setUp() { - parent::setUp(array_merge($this->modules, array( + $widget = static::getWidget(); + + parent::setUp(array( + $widget['module'], 'taxonomy', 'conditional_fields', 'entityreference', 'features', 'uuid', - ))); + )); $this->contentType = $this->drupalCreateContentType(array( 'type' => 'test_content_type', @@ -104,7 +120,7 @@ ), 'field_reference' => array( 'type' => 'entityreference', - 'widget' => $this->getWidget(), + 'widget' => $widget, 'settings' => array( 'handler' => 'base', 'target_type' => 'taxonomy_term', @@ -133,25 +149,6 @@ } /** - * Returns definition of the field widget to test export of. - * - * @return array - * Definition of the widget. - */ - protected function getWidget() { - return array( - 'type' => 'entityreference_autocomplete', - 'active' => TRUE, - 'module' => 'entityreference', - 'settings' => array( - 'match_operator' => 'CONTAINS', - 'size' => 60, - 'path' => '', - ), - ); - } - - /** * Returns the value for the field for the particular widget. * * @param null|string $value diff -u b/src/Tests/EntityReferenceFeaturesUuidExportImport/HierarchicalSelectWidgetTest.php b/src/Tests/EntityReferenceFeaturesUuidExportImport/HierarchicalSelectWidgetTest.php --- b/src/Tests/EntityReferenceFeaturesUuidExportImport/HierarchicalSelectWidgetTest.php +++ b/src/Tests/EntityReferenceFeaturesUuidExportImport/HierarchicalSelectWidgetTest.php @@ -10,20 +10,12 @@ /** * {@inheritdoc} */ - protected $modules = array('hser'); - - /** - * {@inheritdoc} - */ - protected function getWidget() { + protected static function getWidget() { return array( 'type' => 'hser_hierarchy', 'active' => TRUE, 'module' => 'hser', 'settings' => array( - 'match_operator' => 'CONTAINS', - 'size' => 60, - 'path' => '', 'editable' => FALSE, ), ); only in patch2: unchanged: --- a/includes/conditional_fields.features.inc +++ /dev/null @@ -1,126 +0,0 @@ - $data) { - foreach ($data as $dependency => $info) { - $option = $type . ':' . $dependency; - if (!isset($options[$dependency])) { - $options[$option] = $entity_info[$type]['bundles'][$dependency]['label'] . ' (' . $type . ')'; - } - } - } - return $options; -} - -/** - * Implements hook_features_export(). - */ -function conditional_fields_features_export($data, &$export, $module_name = '') { - $export['dependencies']['conditional_fields'] = 'conditional_fields'; - - foreach ($data as $entity) { - $export['features']['conditional_fields'][$entity] = $entity; - } -} - -/** - * Implements hook_features_export_render(). - */ -function conditional_fields_features_export_render($module_name, $data, $export = NULL) { - $dependencies = conditional_fields_load_dependencies(); - if (empty($dependencies)) { - return array(); - } - - $code = array(); - foreach ($data as $option) { - $tokens = explode(':', $option); - $entity = $tokens[0]; - $bundle = $tokens[1]; - - if (isset($dependencies[$entity]) && isset($dependencies[$entity][$bundle])) { - $info = $dependencies[$entity][$bundle]; - - // Ensure that dependencies are always exported in the same order to - // avoid fake overrides. - ksort($info['dependents']); - - $code[] = ''; - $code[] = ' $items["' . $option . '"] = array('; - foreach ($info['dependents'] as $dependent_field_name => $dependees) { - foreach ($dependees as $dependee) { - $code[] = " array("; - $code[] = " 'entity' => '" . $entity . "',"; - $code[] = " 'bundle' => '" . $bundle . "',"; - $code[] = " 'dependent' => '" . $dependent_field_name . "',"; - $code[] = " 'dependee' => '" . $dependee['dependee'] . "',"; - $code[] = " 'options' => " . features_var_export($dependee['options'], ' ') . ','; - $code[] = ' ),'; - } - } - - $code[] = ' );'; - } - } - if (empty($code)) { - return array(); - } - - array_unshift($code, ' $items = array();'); - $code[] = ''; - $code[] = ' return $items;'; - $output = implode("\n", $code); - - return array('conditional_fields_default_fields' => $output); -} - -/** - * Implements hook_features_rebuild(). - */ -function conditional_fields_features_rebuild($module) { - $defaults = features_get_default('conditional_fields', $module); - - if (!empty($defaults)) { - $dependencies = conditional_fields_load_dependencies(); - $instances = field_info_instances(); - foreach ($defaults as $item) { - foreach($item as $field) { - if (isset($dependencies[$field['entity']][$field['bundle']])) { - foreach ($dependencies[$field['entity']][$field['bundle']] as $dependancy_type => $field_dep_settings) { - if ($dependancy_type == 'dependees' && !empty($field_dep_settings)) { - foreach ($field_dep_settings as $dep_setting) { - foreach ($dep_setting as $id => $dep_settings) { - $deps_to_remove[$id] = $id; - } - } - } - } - if (!empty($deps_to_remove)) { - conditional_fields_dependency_delete($deps_to_remove); - } - } - $entity_instances = $instances[$field['entity']][$field['bundle']]; - $dependee_id = $entity_instances[$field['dependee']]['id']; - $dependent_id = $entity_instances[$field['dependent']]['id']; - conditional_fields_dependency_insert($dependee_id, $dependent_id, $field['options']); - } - } - } -} - -/** - * Implements hook_features_revert(). - */ -function conditional_fields_features_revert($module) { - return conditional_fields_features_rebuild($module); -} only in patch2: unchanged: --- /dev/null +++ b/src/Tests/EntityReferenceFeaturesUuidExportImport/SelectListWidgetTest.php @@ -0,0 +1,43 @@ + 'options_select', + 'active' => TRUE, + 'module' => 'options', + ); + } + + /** + * {@inheritdoc} + */ + protected function getWidgetValue($value = NULL) { + if (NULL === $value) { + $value = $this->term->tid; + } + + return array(array('target_id' => $value)); + } + + /** + * {@inheritdoc} + */ + protected function getWidgetValueForm($value = NULL) { + if (NULL === $value) { + $value = $this->term->tid; + } + + return $value; + } + +}