function rules_ui_add_element

Add a new element a rules configuration.

1 string reference to 'rules_ui_add_element'
RulesUIController::config_menu in ui/ui.controller.inc
Generates menu items to manipulate rules configurations.

File

ui/ui.forms.inc, line 261

Code

function rules_ui_add_element($form, &$form_state, $rules_config, $plugin_name, RulesContainerPlugin $parent, $base_path) {
    $cache = rules_get_cache();
    if (!isset($cache['plugin_info'][$plugin_name]['class'])) {
        drupal_not_found();
        exit;
    }
    RulesPluginUI::$basePath = $base_path;
    $plugin_is_abstract = in_array('RulesAbstractPlugin', class_parents($cache['plugin_info'][$plugin_name]['class']));
    // In the first step create the element and in the second step show its edit
    // form.
    if ($plugin_is_abstract && !isset($form_state['rules_element'])) {
        RulesPluginUI::formDefaults($form, $form_state);
        $form_state += array(
            'parent_element' => $parent,
            'plugin' => $plugin_name,
        );
        $form['element_name'] = array(
            '#type' => 'select',
            '#title' => t('Select the %element to add', array(
                '%element' => $plugin_name,
            )),
            '#options' => RulesPluginUI::getOptions($plugin_name),
            '#ajax' => rules_ui_form_default_ajax() + array(
                'trigger_as' => array(
                    'name' => 'continue',
                ),
            ),
        );
        $form['continue'] = array(
            '#type' => 'submit',
            '#name' => 'continue',
            '#value' => t('Continue'),
            '#ajax' => rules_ui_form_default_ajax(),
        );
    }
    elseif (!$plugin_is_abstract) {
        // Create the initial, empty element.
        $element = rules_plugin_factory($plugin_name);
        // Always add the new element at the bottom, thus set an appropriate weight.
        $iterator = $parent->getIterator();
        $iterator_array = iterator_to_array($iterator, TRUE);
        if ($sibling = end($iterator_array)) {
            $element->weight = $sibling->weight + 1;
        }
        $element->setParent($parent);
        $form_state['rules_element'] = $element;
    }
    if (isset($form_state['rules_element'])) {
        $form_state['rules_element']->form($form, $form_state, array(
            'button' => TRUE,
            'init' => TRUE,
        ));
        $form['#validate'][] = 'rules_ui_edit_element_validate';
        $form['#submit'][] = 'rules_ui_edit_element_submit';
    }
    return $form;
}