I want to create a customized kind with a textfield through which I enter some knowledge and it will get saved within the variable desk.
The shape ought to solely include the Add, Take away, and Add extra button so as to add extra textfields.
When the textfield is empty, there must be an Add button; when the textfield is just not empty, there must be a Take away button.
Clicking on the Add extra button ought to create an empty textfield with an Add button.
All this have to occur with out web page reload.
I used to be in a position so as to add the Add, Take away, Add extra, and Submit buttons.
I regarded into the accessible sources and instance modules, however I can not discover useful sources.
Can anybody assist me to change the code in order that the Submit button might be eliminated?
operate mymodule_menu() {
$objects = array();
$objects['admin/mymodule'] = array(
'title' => t('Instance kind'),
'sort' => MENU_CALLBACK,
'web page callback' => 'drupal_get_form',
'web page arguments' => array('mymodule_ajax_example_add_more'),
'entry callback' => TRUE,
);
return $objects;
}
operate mymodule_ajax_example_add_more($kind, &$form_state) {
$mymodule_ajax_events = variable_get('mymodule_ajax_example_content', array());
$mymodule_ajax_events_count = depend($mymodule_ajax_events);
if ($mymodule_ajax_events_count == 0) {
$mymodule_ajax_events_count = 1;
}
$kind = array();
$kind['#tree'] = TRUE;
$kind['mymodule_ajax_events'] = [
'#type' => 'container',
'#weight' => 80,
'#tree' => TRUE,
// Set up the wrapper so that AJAX will be able to replace the fieldset.
'#prefix' => '<div id="js-ajax-elements-wrapper">',
'#suffix' => '</div>',
];
$form_state['field_deltas'] = isset($form_state['field_deltas']) ? $form_state['field_deltas'] : vary(0, $mymodule_ajax_events_count-1);
$field_count = $form_state['field_deltas'];
foreach ($field_count as $delta) {
$kind['mymodule_ajax_events'][$delta] = [
'#type' => 'container',
'#attributes' => [
'class' => ['container-inline'],
],
'#tree' => TRUE,
];
$kind['mymodule_ajax_events'][$delta]['event_url'] = [
'#type' => 'textfield',
'#title' => t('Event URL' . ($delta + 1)),
'#size' => 40,
'#default_value' => (isset($mymodule_ajax_events[$delta])) ? $mymodule_ajax_events[$delta] : '',
];
$kind['mymodule_ajax_events'][$delta]['remove_event_url'] = array(
'#sort' => 'submit',
'#worth' => t('Take away'),
'#submit' => ['mymodule_ajax_example_add_more_remove'],
'#ajax' => [
'callback' => 'mymodule_ajax_example_add_more_remove_callback',
'wrapper' => 'js-ajax-elements-wrapper',
],
'#weight' => 50,
'#attributes' => [
'class' => ['button-small'],
],
'#identify' => 'remove_event_url_' . $delta,
);
}
$kind['mymodule_ajax_events']['add_event_url'] = array(
'#sort' => 'submit',
'#worth' => t('Add yet one more'),
'#submit' => ['mymodule_ajax_example_add_more_add_one'],
'#ajax' => [
'callback' => 'mymodule_ajax_example_add_more_add_one_callback',
'wrapper' => 'js-ajax-elements-wrapper',
],
'#weight' => 1,
);
$kind['submit'] = array(
'#sort' => 'submit',
'#worth' => t('Submit'),
'#weight' => 100,
);
return $kind;
}
operate mymodule_ajax_example_add_more_remove($kind, &$form_state) {
$delta_remove = $form_state['triggering_element']['#parents'][1];
$ok = array_search($delta_remove, $form_state['field_deltas']);
unset($form_state['field_deltas'][$k]);
$form_state['rebuild'] = TRUE;
drupal_get_messages();
}
operate mymodule_ajax_example_add_more_remove_callback($kind, &$form_state) {
return $kind['mymodule_ajax_events'];
}
operate mymodule_ajax_example_add_more_add_one($kind, &$form_state) {
$form_state['field_deltas'][] = depend($form_state['field_deltas']) > 0 ? max($form_state['field_deltas']) + 1 : 0;
//mymodule_ajax_example_add_more_validate();
$form_state['rebuild'] = TRUE;
// drupal_get_messages();
}
operate mymodule_ajax_example_add_more_add_one_callback($kind, $form_state) {
return $kind['mymodule_ajax_events'];
}
operate mymodule_ajax_example_add_more_validate($kind, $form_state) {
dpm($form_state);
foreach ($form_state['values']['mymodule_ajax_events'] as $key => $merchandise) {
//dpm($merchandise);
dpm($merchandise['event_url']);
if (!empty($merchandise['event_url'])) {
if (!preg_match('/https://instance.com.*/i', $merchandise['event_url'])) {
form_set_error("mymodule_ajax_events][$key][event_url", 'Enter a valid Event URL.' . ($key + 1));
//
}else{
drupal_set_message('URL ' . ($key + 1) . ' Created' );
}
}
}
}
function mymodule_ajax_example_add_more_submit($form, $form_state) {
$events_content = array();
$events_count = 0;
foreach ($form_state['values']['mymodule_ajax_events'] as $key => $merchandise) {
if (!empty($merchandise['event_url'])) {
$events_content[] = $merchandise['event_url'];
$events_count++;
}
}
variable_set('mymodule_ajax_example_content', $events_content);
variable_set('mymodule_ajax_example_count', $events_count);
}