Skip to content

Commit

Permalink
Added hook_TYPE_alter() and make all entity fields available in form …
Browse files Browse the repository at this point in the history
…element list.
  • Loading branch information
kenorb committed Dec 15, 2015
1 parent fb47b4c commit 2681a23
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 22 deletions.
37 changes: 37 additions & 0 deletions rules_forms.api.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

/**
* @file
* Hooks provided by the rules_forms module.
*
* @ingroup xmlsitemap
*/

/**
* @addtogroup hooks
* @{
*/


/**
* Implements hook_TYPE_alter().
*
* Alter the current form_info data of rules_forms.
*/
function hook_rules_forms_form_info_alter(&$form_info, $context) {

$module = 'foo_module';
foreach ($context['fields'] as $key => $field) {
if ($field['widget']['module'] == $module) {
$form_info['elements'][$module . ':' . $module] = array(
'type' => $module,
'label' => $field['label'],
);
}
}

}

/**
* @} End of "addtogroup hooks".
*/
38 changes: 16 additions & 22 deletions rules_forms.module
Original file line number Diff line number Diff line change
Expand Up @@ -282,30 +282,24 @@ function rules_forms_after_build($form, &$form_state) {
if ($entity_info['fieldable']) {
$fields = field_info_instances($entity_type, $bundle);

if (module_exists('field_collection')) {
// Loop through fields, grabbing field_collection ones, so we build a suitable form_info listing.
foreach ($fields as $k => $field) {
if (isset($field['widget'])) {
if ($field['widget']['module'] === 'field_collection') {
$t = 'field_collection';
$element_id = $t . ':' . $k;
$form_info['elements'][$element_id] = array(
'type' => $t,
'label' => $field['label'],
);
}
}

// @todo: Think about clearing out field_collections, but wait to see behaviours first.

}
// Append all available fields to the list.
foreach ($fields as $key => $field) {
$module = $field['widget']['module'];
$form_info['elements'][$module . ':' . $key] = array(
'type' => $module,
'label' => $field['label'],
);
}

if (module_exists('field_group') && function_exists('field_group_read_groups')) {
// Enabled groups returned only be default @see field_group_read_groups()
$groups = field_group_read_groups(['bundle' => $form['#bundle']]);

}
// Allow alter form_info, so modules can add new custom fields.
$context = array(
'form' => $form,
'fields' => $fields,
// Enabled groups returned only be default
// @see field_group_read_groups()
'groups' => module_exists('field_group') ? field_group_read_groups(['bundle' => $form['#bundle']]) : array(),
);
drupal_alter('rules_forms_form_info', $form_info, $context);

$elements = array_intersect(array_keys($fields), array_values(element_children($form)));
foreach ($elements as $e) {
Expand Down

0 comments on commit 2681a23

Please sign in to comment.