【问题标题】:Drupal 8 select field in blockDrupal 8选择块中的字段
【发布时间】:2017-07-09 07:07:30
【问题描述】:

您好,我正在创建的模块中有这段代码,我在控制器类的函数中输出:

$output ="
        <span id="dateofshow"> Date of Show:<select id="showdate" class="js-
    showtimes-select Showtimes-select">'
        <option value="2017-07-07">Sat, Jul  7</option>
        <option value="2017-07-08">Sat, Jul  8</option>
        <option value="2017-07-09">Sat, Jul  9</option>
        etc...
        </select></span>"
return output;

当我将它输出到它自己的页面时它会正确显示,但是当我把它变成一个块时它会被剥离出来,看起来像这样:

<span id="dateofshow"> Date of Show: Fri, Jul  7Sat, Jul  8Sun, Jul  9Mon, Jul  10Tue, Jul  11Wed, Jul  12Thu, Jul  13Fri, Jul  14Sat, Jul  15Sun, Jul  16Mon, Jul  17Tue, Jul  18Wed, Jul  19Thu, Jul  20Wed, Jul  26Thu, Jul  27Wed, Aug  2Thu, Aug  3Wed, Aug  9Thu, Aug  10Fri, Aug  25Sat, Aug  26Sun, Aug  27Mon, Aug  28Tue, Aug  29Wed, Aug  30Thu, Aug  31</span>

此外,style="display: block;" 标记也以块形式被剥离,并且在此代码的同一函数中:

<div id="2017-07-07" class="js-showtimes-date" style="display: block;">

在 Drupal 8 中执行此操作的正确方法是什么?

更新:这是我使用的代码,其中一些不相关的部分被删掉了:

tlistingcontroller.php

namespace Drupal\ttimes\Controller;

use Symfony\Component\HttpFoundation\Response;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Session\AccountInterface;
use \DateTime;

class tlistingsController 
{
  public function buildtimes(){
  $output = "";

xmlpath = \Drupal::service('file_system')->realpath(file_default_scheme() . "://").'/x/'.'siteId.xml';
$schedResult = simplexml_load_file($xmlpath);

  $output .= <span id="dofshow"> Date of Show: <select id="showd" class="js-showtimes-select Showtimes-select">';
  foreach($schedResult->xpath('//EBDates/EBDate') as $dates){
            $dateformat = date_format ( new DateTime($dates->BDate) , 'D, M  j');
            $output .= '<option value="'. $dates->BDate . '">'. $dateformat . '</option>';
  $output .= '</select></span>';
  return $output;
}
public function tlistings_page() {  //This is what gets called in the .routing.yml file
    return array(
      '#type' => 'markup',
      '#attached' => array(
        'library' => array(
          'tlisting/global',),),
      '#markup' => $this->buildtimes(),
    );  
    }
}

自从我第一次发布它以来,我通过添加一个带有一些 css 和 js 的库来更新它,这样我就可以做到这一点。该函数读取 xml 文件,然后从中输出数据,部分数据是它提取并放入 select 和 options 标签的日期。如何使用选择表单,但将其与从函数中提取的数据放在当前相同的位置?

【问题讨论】:

  • 向我们展示您的代码?

标签: javascript drupal-modules drupal-8


【解决方案1】:

出于安全原因,drupal 去除了 html 和内联样式。更好的方法是使用表单 API 并实现 FormBase 类。

FormAPI Drupal 8

你也应该看看这个tutorial

【讨论】:

  • 感谢我对来自早期版本的 Drupal 8 开发还是新手。我会仔细阅读它。我发现我可以在渲染中使用'#allowed_tags' => ['option', 'select', 'a', 'div'] 来避开这个问题,但我想学习drupal 的方法来做到这一点。我会研究这个。谢谢。
【解决方案2】:

如果你想保持跨度,你可以使用前缀和后缀:

$options['2017-07-07'] = 'Sat, Jul  7';
$options['2017-07-08'] = 'Sat, Jul  8';
$options['2017-07-09'] = 'Sat, Jul  9';

$form['showdate'] = array(
  '#type' => 'select',
  '#options' => $options,
  '#required' => TRUE,
  '#title' => $this->t('Date of Show:'),
  '#prefix' => '<span id="dateofshow">',
  '#suffix' => '</span>');

据我了解,您必须使用渲染数组上的属性来获取要输出的内联样式。

  $form['showdate'] = array(
  '#type' => 'select',
  '#options' => $options,
  '#required' => TRUE,
  '#title' => $this->t('Date of Show:'),
  '#prefix' => '<span id="dateofshow">',
  '#suffix' => '</span>',
  '#attributes' => array ('style' => 'display: block;'));

我的建议不是对抗 Drupal 输出的标记,而是使用它。如果您提供更多关于您想要实现的目标的信息,我将很乐意扩展。

更新: 这里还有一些代码可能会根据您的更新对您有所帮助。我没有测试过。

$xmlpath = \Drupal::service('file_system')->realpath(file_default_scheme() . "://").'/x/'.'siteId.xml';
  $schedResult = simplexml_load_file($xmlpath);
  $options = array();
  foreach($schedResult->xpath('//EBDates/EBDate') as $dates)
  {
    $options[$dates->BDate] = date_format ( new DateTime($dates->BDate) , 'D, M  j');
  }
  $form['showdate'] = array(
    '#type' => 'select',
    '#options' => $options,
    '#required' => TRUE,
    '#title' => $this->t('Date of Show:'),
    '#attributes' => array (
      'class' => array(
        'js-showtimes-select',
        'Showtimes-select')));

【讨论】:

  • 我读了一些关于以这种方式创建表单的内容,但我不明白的部分是如何在我的函数的输出中间返回表单。此外,我的函数具有来自填充表单的 xml 文件的数据,我将如何在其中获取它?我用更多关于我正在做的事情的代码更新了我的原始帖子。感谢您的帮助!
  • 谢谢,我会解决的。
  • 添加了另一个可能有用的示例。 @vishwa 在将东西包装在跨度方面是正确的。正如我在原始答案中建议的那样,尝试使用 Drupal 标记,而不是反对它,你的生活会更轻松,我是根据经验说的:S
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-11-25
  • 1970-01-01
  • 1970-01-01
  • 2018-10-26
  • 1970-01-01
  • 2018-11-22
  • 2012-02-15
相关资源
最近更新 更多