如何在Drupal 7中使用Add value,Remove按钮为多值字段表单创建一个没有提交按钮的Ajax表单?

时间:2018-06-01 10:43:56

标签: php drupal drupal-7 drupal-modules

您好我想创建一个自定义Drupal表单,其中有一个textfiled,我在其中输入一些数据并将其保存在变量表中。

我的要求是表单应该只包含AddRemoveAdd More按钮以添加其他文本文件。

当textfiled为空时,它应该显示textfiled旁边的Add按钮(当输入数据并单击Add按钮时它应该将数据保存在变量表中),当textfiled不为空时,它应该显示textfiled旁边的remove按钮(当点击时删除按钮应该删除变量表中的数据)

点击Add More按钮需要创建一个空白文本,并在其旁边显示Add按钮。

所有这些都需要在没有页面重新加载的情况下发生。

我能够通过AddRemoveAdd MoreSubmit按钮完成任务。

我查看了可用的资源和示例模块,但无法找到有用的资源。

任何人都可以帮我修改代码,以便删除Submit按钮。

我的代码如下

<?php
function mymodule_menu() {

    $items = array();

    $items['admin/mymodule'] = array(
        'title' => t('Example form'),
        'type' => MENU_CALLBACK,
        'page callback' => 'drupal_get_form',
        'page arguments' => array('mymodule_ajax_example_add_more'),
        'access callback' => TRUE,
    );

    return $items;
}

function mymodule_ajax_example_add_more($form, &$form_state) {

  $mymodule_ajax_events = variable_get('mymodule_ajax_example_content', array());
  $mymodule_ajax_events_count = count($mymodule_ajax_events);

  if ($mymodule_ajax_events_count == 0) {
    $mymodule_ajax_events_count = 1;
  }

  $form = array();
  $form['#tree'] = TRUE;
  $form['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'] : range(0, $mymodule_ajax_events_count-1);

  $field_count = $form_state['field_deltas'];
  foreach ($field_count as $delta) {
    $form['mymodule_ajax_events'][$delta] = [
      '#type' => 'container',
      '#attributes' => [
        'class' => ['container-inline'],
      ],
      '#tree' => TRUE,
    ];

    $form['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] : '',
    ];

    $form['mymodule_ajax_events'][$delta]['remove_event_url'] = array(
      '#type' => 'submit',
      '#value' => t('Remove'),
      '#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'],
      ],
      '#name' => 'remove_event_url_' . $delta,
    );
  }

  $form['mymodule_ajax_events']['add_event_url'] = array(
    '#type' => 'submit',
    '#value' => t('Add 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,
  );
    $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Submit'),
    '#weight' => 100,
  );

  return $form;
}

function mymodule_ajax_example_add_more_remove($form, &$form_state) {
  $delta_remove = $form_state['triggering_element']['#parents'][1];
  $k = array_search($delta_remove, $form_state['field_deltas']);
  unset($form_state['field_deltas'][$k]);

  $form_state['rebuild'] = TRUE;
  drupal_get_messages();
}

function mymodule_ajax_example_add_more_remove_callback($form, &$form_state) {
  return $form['mymodule_ajax_events'];
}

function mymodule_ajax_example_add_more_add_one($form, &$form_state) {
  $form_state['field_deltas'][] = count($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();
}

function mymodule_ajax_example_add_more_add_one_callback($form, $form_state) {
  return $form['mymodule_ajax_events'];
}

function mymodule_ajax_example_add_more_validate($form, $form_state) {
    dpm($form_state);
  foreach ($form_state['values']['mymodule_ajax_events'] as $key => $item) {
      //dpm($item);
      dpm($item['event_url']);
   if (!empty($item['event_url'])) {
      if (!preg_match('/https:\/\/example.com.*/i', $item['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 => $item) {
    if (!empty($item['event_url'])) {
      $events_content[] = $item['event_url'];
      $events_count++;
   }
 }
  variable_set('mymodule_ajax_example_content', $events_content);
  variable_set('mymodule_ajax_example_count', $events_count);
}

0 个答案:

没有答案