如何保留 Drupal 表单“标记”?从“提交”的包装器内呈现的元素元素?

发布于 2024-12-18 15:09:10 字数 1760 浏览 0 评论 0原文

我已经以编程方式向 Drupal 表单添加了“标记”元素。当它在页面上呈现时,该元素将出现在提交按钮的包装器中。 澄清:它应该出现在“field_school_name_value”元素和“distance”元素之间。我已经设置了每个元素的权重,希望这会强制布局是的,但似乎没有帮助。我做错了什么?

<?php
function abq_misc_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == 'views_exposed_form') {
    $state_select = array(
      '#attributes' => array(
        'style' => 'width:10em;',
      ),
      '#default_value' => 'All',
      '#multiple' => FALSE,
      '#options' => array_merge(array('All' => 'State'), location_get_provinces()),
      '#title' => NULL,
      '#type' => 'select',
      '#weight' => 0,
    );
    $form['province'] = $state_select;

    $school = &$form['field_school_name_value'];
    $school['#attributes'] = array(
      'size' => 15,
    );
    $school['#weight'] = 1;

    // THIS GUY
    $form['divider'] = array(
      '#type' => 'item',
      '#markup' => '<div>&ndash;or&ndash;</div>',
      '#weight' => 2,
    );

    $form['distance']['#weight'] = 3;

    $search_distance = &$form['distance']['search_distance'];
    $search_distance['#attributes'] = array(
      'placeholder' => 'miles',
      'size' => '5',
    );
    $search_distance['#prefix'] = 'Within';
    $search_distance['#suffix'] = 'of';
    unset($search_distance['#title']);
    $search_distance['#weight'] = 0;

    $postal_code = &$form['distance']['postal_code'];
    unset($postal_code['#title']);
    $postal_code['#attributes'] = array(
      'placeholder' => 'Zip Code',
      'size' => '5',
    );
    $postal_code['#weight'] = 1;

    hide($form['distance']['search_units']);

    $form['submit']['#weight'] = 4;
 }

}

I have programatically added a "markup" element to a Drupal form. When it renders on the page, the element appears in the wrapper for the submit button. Clarification: It should appear between the 'field_school_name_value' element and the 'distance' element. I have set the weights of every element in hopes that that would force the layout to be right, but it doesn't seems to help. What am I doing wrong?

<?php
function abq_misc_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == 'views_exposed_form') {
    $state_select = array(
      '#attributes' => array(
        'style' => 'width:10em;',
      ),
      '#default_value' => 'All',
      '#multiple' => FALSE,
      '#options' => array_merge(array('All' => 'State'), location_get_provinces()),
      '#title' => NULL,
      '#type' => 'select',
      '#weight' => 0,
    );
    $form['province'] = $state_select;

    $school = &$form['field_school_name_value'];
    $school['#attributes'] = array(
      'size' => 15,
    );
    $school['#weight'] = 1;

    // THIS GUY
    $form['divider'] = array(
      '#type' => 'item',
      '#markup' => '<div>–or–</div>',
      '#weight' => 2,
    );

    $form['distance']['#weight'] = 3;

    $search_distance = &$form['distance']['search_distance'];
    $search_distance['#attributes'] = array(
      'placeholder' => 'miles',
      'size' => '5',
    );
    $search_distance['#prefix'] = 'Within';
    $search_distance['#suffix'] = 'of';
    unset($search_distance['#title']);
    $search_distance['#weight'] = 0;

    $postal_code = &$form['distance']['postal_code'];
    unset($postal_code['#title']);
    $postal_code['#attributes'] = array(
      'placeholder' => 'Zip Code',
      'size' => '5',
    );
    $postal_code['#weight'] = 1;

    hide($form['distance']['search_units']);

    $form['submit']['#weight'] = 4;
 }

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我是有多爱你 2024-12-25 15:09:10

我不确定为什么会发生这种情况,没有充分的理由将您的元素呈现在提交按钮的包装器内。

不过,一个简单的解决方法是在提交按钮上使用 #prefix 属性,这将保证您的标记在提交按钮的包装器之前立即呈现:

$form['submit']['#prefix'] = '<div>–or–</div>';

UPDATE

只是为了解决您的编辑问题,我认为如果您设置 distance 元素的 #prefix ,则可以应用相同的解决方案:

$form['distance']['#prefix'] = '<div>–or–</div>';

其他模块可能会完成一些额外的格式化实现hook_form_alter 它恰好在你的后面运行,搞乱了你的良好工作。通过将前缀应用于 distance 元素,您将确保它紧邻 field_school_name_value 元素之前。

我应该提到,在引用所提供的 $form 的数组成员时,我遇到了问题(您正在使用 $school = &$form[ 'field_school_name_value'];)。作为额外的健全性检查,我建议将该代码更改为此,看看它是否有帮助(在上面的其他建议之前尝试这个,因为它可能会修复它):

$form['field_school_name_value']['#attributes'] = array('size' => 15);
$form['field_school_name_value']['#weight'] = 1;

而不是

$school = &$form['field_school_name_value'];
$school['#attributes'] = array(
  'size' => 15,
);
$school['#weight'] = 1;

I'm not sure why that's happening, there's no good reason for your element to be rendered inside the wrapper for the submit button.

An easy fix, though, would be to use the #prefix attribute on the submit button which would guarantee that your markup was rendered immediately before the wrapper for the submit button:

$form['submit']['#prefix'] = '<div>–or–</div>';

UPDATE

Just to address your edit, I think the same solution can apply if you set the #prefix of the distance element instead:

$form['distance']['#prefix'] = '<div>–or–</div>';

It's possible that there's some extra formatting done by another module that implements hook_form_alter which happens to run after yours, messing up your good work. By applying the prefix to the distance element you'll be ensuring it comes immediately before the field_school_name_value element.

I should mention that I've had problems with exactly this when taking a reference to an array member of the provided $form (which you're doing with $school = &$form['field_school_name_value'];). As an extra sanity check I'd recommend changing that bit of code to this and see if it helps (try this before the other suggestion above as it might just fix it):

$form['field_school_name_value']['#attributes'] = array('size' => 15);
$form['field_school_name_value']['#weight'] = 1;

instead of

$school = &$form['field_school_name_value'];
$school['#attributes'] = array(
  'size' => 15,
);
$school['#weight'] = 1;
复古式 2024-12-25 15:09:10

虽然这是一个快7年的老问题了,但我只是运行这个问题。
这是解决方案: https://www.drupal.org/project/views/issues /2070533

首先,您应该在 form_alter 挂钩中声明标记,但必须在定义数组中声明 '#printed' 键。然后您应该在模块或主题中实现 template_preprocess_views_exposed_form() 函数。这里你可以通过drupal_render渲染所需的表单元素并将其添加到$variables中。最后,您可以打印主题中的变量(作为自定义变量或在小部件中)。
但最简单的版本:将views-exposed-form.tpl.php克隆到主题的模板目录中,按照建议命名(例如views-exposed-form--news.tpl.php)并在其中打印换行符你想要的。

Although this is an almost 7 years old issue, I just run this problem.
Here is the solution: https://www.drupal.org/project/views/issues/2070533

First, you should declare your markup in your form_alter hook but the '#printed' key must be declared in your defining array. Then you should implement the template_preprocess_views_exposed_form() function in your module or in theme. Here you can render the required form element by drupal_render and add it to $variables. And finally you can print out the variable in the theme (as custom variable or among widgets).
But the simpliest version: clone the views-exposed-form.tpl.php to the template directory of your theme, name it as is suggested (eg. views-exposed-form--news.tpl.php) and print your linebreak where you want.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文