在Drupal7中扩展字段集时,修改collapse.js以从xml获取附加数据

Modify collapse.js to get addtional data from xml when expanding fieldset in Drupal 7?

本文关键字:xml 以从 js 获取 数据 collapse 修改 扩展 Drupal7 字段      更新时间:2023-09-26

在drupal中,我生成了一个列表,其中每个项目都是一个可折叠的字段集,可以包含额外的信息。

由于列表相当大,我希望在用户单击字段集之前避免加载额外的信息。

最佳情况:

用户单击折叠的字段集。字段集加载额外信息。字段集未失效。

我已经将collapse.js的副本复制并加载到我的表单中,但我对js和jQuery非常陌生,所以我有点迷失了方向。如果有人能告诉我如何在第一次扩展字段集时调用函数,我相信我能解决剩下的问题

我已经包含了collapse.js:中的代码

(function ($) {
//Toggle the visibility of a fieldset using smooth animations.
Drupal.toggleFieldset = function (fieldset) {
  var $fieldset = $(fieldset);
   if ($fieldset.is('.collapsed')) {
    var $content = $('> .fieldset-wrapper', fieldset).hide();
    $fieldset
      .removeClass('collapsed')
      .trigger({ type: 'collapsed', value: false })
      .find('> legend span.fieldset-legend-prefix').html(Drupal.t('Hide'));
    $content.slideDown({
      duration: 'fast',
      easing: 'linear',
      complete: function () {
        Drupal.collapseScrollIntoView(fieldset);
        fieldset.animating = false;
      },
      step: function () {
        // Scroll the fieldset into view.
        Drupal.collapseScrollIntoView(fieldset);
      } 
    });
  }
  else {
    $fieldset.trigger({ type: 'collapsed', value: true });
    $('> .fieldset-wrapper', fieldset).slideUp('fast', function () {
      $fieldset
        .addClass('collapsed')
        .find('> legend span.fieldset-legend-prefix').html(Drupal.t('Show'));
       fieldset.animating = false;
    });
  }
};
//Scroll a given fieldset into view as much as possible.
Drupal.collapseScrollIntoView = function (node) {
  var h = document.documentElement.clientHeight || document.body.clientHeight || 0;
  var offset = document.documentElement.scrollTop || document.body.scrollTop || 0;
  var posY = $(node).offset().top;
  var fudge = 55;
  if (posY + node.offsetHeight + fudge > h + offset) {
    if (node.offsetHeight > h) {
      window.scrollTo(0, posY);
    }
    else {
      window.scrollTo(0, posY + node.offsetHeight - h + fudge);
    }
  }
};
Drupal.behaviors.collapse = {
  attach: function (context, settings) {
    $('fieldset.collapsible', context).once('collapse', function () {
      var $fieldset = $(this);
      // Expand fieldset if there are errors inside, or if it contains an
      // element that is targeted by the uri fragment identifier. 
      var anchor = location.hash && location.hash != '#' ? ', ' + location.hash : '';
      if ($('.error' + anchor, $fieldset).length) {
        $fieldset.removeClass('collapsed');
      }
      var summary = $('<span class="summary"></span>');
      $fieldset.
        bind('summaryUpdated', function () {
          var text = $.trim($fieldset.drupalGetSummary());
           summary.html(text ? ' (' + text + ')' : '');
        })
        .trigger('summaryUpdated');
      // Turn the legend into a clickable link, but retain span.fieldset-legend
      // for CSS positioning.
      var $legend = $('> legend .fieldset-legend', this);
      $('<span class="fieldset-legend-prefix element-invisible"></span>')
        .append($fieldset.hasClass('collapsed') ? Drupal.t('Show') : Drupal.t('Hide'))
        .prependTo($legend)
        .after(' ');
      // .wrapInner() does not retain bound events.
      var $link = $('<a class="fieldset-title" href="#"></a>')
        .prepend($legend.contents())
        .appendTo($legend)
        .click(function () {
          var fieldset = $fieldset.get(0);
          // Don't animate multiple times.
          if (!fieldset.animating) {
            fieldset.animating = true;
            Drupal.toggleFieldset(fieldset);
          }
          return false;
        });
       $legend.append(summary);
    });
  }
};
})(jQuery);

在我看来,你必须重写整个Drupal.toggleFieldset函数(就像重写Drupal主题函数一样。

您可以在FormAPI中向字段集添加一个类,然后在$content.slideDown参数的complete函数中捕获它,并激发您的自定义函数,以添加"加载"图形并发出ajax请求。

根据您的问题,我假设您对FormAPI/jQuery.ajax()足够熟悉,可以尝试一下。但如果没有,请告诉我,我会包括一些片段

编辑

这里有一些示例代码,为此设置测试环境需要很长时间,所以它只是一个指针(不能为此创建JS fiddle;)

您可以在PHP 中添加这样的字段集

$form['my_fieldset'] = array(
  '#type' = 'fieldset',
  '#title' = t('My fieldset'),
  '#collapsible' = true,
  '#collapsed' = true,
  '#attributes' => array(
    'class' => array('ajax-fieldset'),
    'rel' => 'callback/url/path' // random attribute to store the link to a menu path that will return your HTML
  )
);
$form['my_fieldset'] = array(
  '#markup' => '<div class="response">loading...</div>'
);

很明显,您还将设置一个返回主题数据@callback/url/path的菜单挂钩。IMO最好返回JSON数据并使用JS模板将其主题化,但Drupal的方式(至少目前)似乎是在菜单挂钩回调函数中呈现HTML。

然后是JS。我只包含了修改后的完整函数,而不是复制您粘贴的内容。将完整的函数添加到代码副本中,在您自己的JS文件中重新指定核心Drupal函数

$content.slideDown({
  complete: function () {
    Drupal.collapseScrollIntoView(fieldset);
    fieldset.animating = false;
    if($fieldset.hasClass('ajax-fieldset')) {
      $.get(
        Drupal.settings.basePath + $fielset.attr('rel'),
        function(data) {
          $fieldset.find('.response').html(data);
        }
      )
    }
  }
});

或者,与其摆弄可折叠功能。只需在没有可折叠/折叠类的情况下创建自己的字段集,并从头开始实现

所以…类似的东西:)