【发布时间】:2016-08-07 05:02:05
【问题描述】:
我想使用Drupal 8 中的块模块构建一个表单。我知道在 Drupal 7 中构建表单,但在 Drupal 8 中似乎有所不同。
请求任何参与过 drupal8 自定义表单的人帮助我。
【问题讨论】:
我想使用Drupal 8 中的块模块构建一个表单。我知道在 Drupal 7 中构建表单,但在 Drupal 8 中似乎有所不同。
请求任何参与过 drupal8 自定义表单的人帮助我。
【问题讨论】:
要使用块模块构建表单,您可以轻松使用Webform module,您可以在其中添加表单并显示为块。
如果您打算在自定义块中以编程方式创建表单,您可以通过创建如下所示的两个文件来实现:
表单文件(src/Form/DemoForm.php):
<?php
/**
* @file
* Contains \Drupal\demo\Form\DemoForm.
*/
namespace Drupal\demo\Form;
use Drupal\Core\Form\FormBase;
class DemoForm extends FormBase {
/**
* {@inheritdoc}.
*/
public function getFormId() {
return 'demo_form';
}
/**
* {@inheritdoc}.
*/
public function buildForm(array $form, array &$form_state) {
$form['email'] = array(
'#type' => 'email',
'#title' => $this->t('Your .com email address.')
);
$form['show'] = array(
'#type' => 'submit',
'#value' => $this->t('Submit'),
);
return $form;
}
/**
* {@inheritdoc}
*/
public function validateForm(array &$form, array &$form_state) {
$values = $form_state->getValues();
if (strpos($values['email'], '.com') === FALSE ) {
$form_state->setErrorByName('email', t('This is not a .com email address.'));
}
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, array &$form_state) {
drupal_set_message($this->t('Your email address is @email', array('@email' => $form_state['values']['email'])));
}
}
来源:Building a Drupal 8 Module: Blocks and Forms.
块文件(src/Plugin/Block/HelloBlock.php):
<?php
namespace Drupal\mymodule\Plugin\Block;
use Drupal\Core\Block\BlockBase;
/**
* Provides a 'Hello' Block.
*
* @Block(
* id = "form_block",
* admin_label = @Translation("My form"),
* category = @Translation("My Category"),
* )
*/
class HelloBlock extends BlockBase {
/**
* {@inheritdoc}
*/
public function build() {
$form = \Drupal::formBuilder()->getForm('\Drupal\mymodule\Form\HelloBlock');
//$form['#attached']['js'][] = drupal_get_path('module', 'example') . '/js/example.js';
//$form['#markup'] = $this->t('Custom text');
return $form;
}
}
要将表单添加到块配置,请参阅:Add a Form to the Block Configuration。
【讨论】:
您的问题非常模糊,因为我不知道您已经对 Drupal 8 中的模块、表单和块了解多少。所以这里有一个小指南如何做,更多关于如何做的详细信息会这个答案有点矫枉过正。
1.创建一个新模块并启用它
看这里:Naming and placing your Drupal 8 module。
基本上你创建模块文件夹和模块信息 yml 文件让 Drupal 知道模块。然后使用 drush 或 Drupal 中的管理区域启用它。
2。创建表单
在your_module/src/Form 下创建表单。上面的链接中的更多详细信息。
3。创建块并渲染表单
在your_module/src/Plugin/Block/ 下创建将呈现表单的块。
这个想法基本上是(根据 Henrik 的建议更新了代码):
$builtForm = \Drupal::formBuilder()->getForm('Drupal\your_module\Form\YourForm');
$renderArray['form'] = $builtForm;
return $renderArray;
注意:您不需要用$renderArray 包装$builtForm,您可以只返回$builtForm 就可以了。我个人只是喜欢这样做,因为我经常需要在最终的渲染数组中添加一些其他东西,比如一些标记、缓存设置或库等。
4.放置方块
将块放置在所需区域。完成。
【讨论】:
$builtForm = \Drupal::formBuilder()->getForm('Drupal\yourmodule\Form\YourForm'); 构建表单 - 无论如何,很好的答案。
以下是有关如何进行此操作的详细摘要:-
https://www.sitepoint.com/building-drupal-8-module-blocks-forms/
按照上述指南,您可以将完成的表单添加到块构建函数中,例如
class DemoBlock extends BlockBase {
/**
* {@inheritdoc}
*/
public function build() {
$form = \Drupal::formBuilder()->getForm('Drupal\demo\Form\DemoForm');
return $form;
}
}
如果您是 Drupal 8 的新手或需要重新学习知识,可以参考一些更有用的文档:
https://www.drupal.org/docs/8/creating-custom-modules
【讨论】: