Drupal 8:自定义块中的自定义构建iFrame不显示

时间:2019-04-04 05:56:29

标签: drupal drupal-8

在D8中,我以编程方式创建了一个块,并根据数据库值创建了一个iFrame。以下是该代码段的代码:

class GooglemapBlock extends BlockBase {

  /**
   * {@inheritdoc}
   */
  public function build() {
    $build = [];
    //$build['googlemap_block']['#markup'] = 'Implement GooglemapBlock.';
    $config = \Drupal::config('custom.default');
    $build['googlemap_block']['#markup'] = drupal_set_message($config->get('google_token'));
    $rawTxt = $build['googlemap_block']['#markup']['status'][0];
    $build['googlemap_block']['#markup'] = '<iframe src="'.$rawTxt.'" width="100%" height="100%" frameborder="0"allowfullscreen></iframe>';

    return $build;
  }

}

然后我从管理面板将块分配到了footer_first区域。

现在,在page.html.twig中,我使用了与{{ page.footer_first }}相同的名称,但是,我看到它显示了一个空格。没有iFrame显示。

我要在iFrame中传递的存储数据库值如下:

https://www.google.com/maps/embed?pb=!1m18!1m12!1m3!1d9207.358598888495!2d-85.64847801496286!3d30.183918972289003!2m3!1f0!2f0!3f0!3m2!1i1024!2i768!4f13.1!3m3!1m2!1s0x0000000000000000%3A0x2320479d70eb6202!2sDillard!5e0!3m2!1sbn!2sbd!4v1462359735720

1 个答案:

答案 0 :(得分:0)

根据https://drupal.stackexchange.com/questions/246151/iframe-not-displayed-in-drupal-8-controller-page的回答,我在自定义代码段中执行了以下操作,现在可以正常使用了:

class GooglemapBlock extends BlockBase {

  /**
   * {@inheritdoc}
   */
  public function build() {
    $build = [];
    //$build['googlemap_block']['#markup'] = 'Implement GooglemapBlock.';
    $config = \Drupal::config('custom.default');
    $build['googlemap_block']['#markup'] = drupal_set_message($config->get('google_token'));
    $rawTxt = $build['googlemap_block']['#markup']['status'][0];

    $iframe = [];
    $iframe = array(
      '#type' => 'inline_template',
      '#template' => '<iframe width="100%" height="100%" src="'.$rawTxt.'" 
       frameborder="0" allowfullscreen></iframe>',
      //'#context' => ['url' => 'https://www.youtube.com/embed/yAoLSRbwxL8',],
    );

    return $iframe;
  }

}