在D8中,我想在自定义生成的块中显示一个自定义表单。我已经创建了所需的自定义模块,并为该块分配了一个区域。如果以管理员或经过身份验证的用户身份登录,则前端的任何区域都不会显示任何内容。但是,如果我将前端视为匿名用户,则表单即将到来。下面是我的代码。需要您的帮助。
namespace Drupal\dash\Plugin\Block;
use Drupal\Core\Block\BlockBase;
/**
* Provides a 'Dash' Block.
*
* @Block(
* id = "dash_block",
* admin_label = @Translation("Dash block"),
* category = @Translation("Dash"),
* )
*/
class DashBlock extends BlockBase {
/**
* {@inheritdoc}
*/
public function build() {
$form = \Drupal::formBuilder()->getForm('Drupal\dash\Form\WorkForm');
return $form;
}
}
/**
* @file
* Contains \Drupal\dash\Form\WorkForm.
*/
namespace Drupal\dash\Form;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
class WorkForm extends FormBase {
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'dash_form';
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$form['employee_name'] = array(
'#type' => 'textfield',
'#title' => t('Employee Name:'),
'#required' => TRUE,
);
$form['employee_mail'] = array(
'#type' => 'email',
'#title' => t('Email ID:'),
'#required' => TRUE,
);
$form['actions']['#type'] = 'actions';
$form['actions']['submit'] = array(
'#type' => 'submit',
'#value' => $this->t('Register'),
'#button_type' => 'primary',
);
return $form;
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
drupal_set_message($this->t('@emp_name ,Your application is being submitted!', array('@emp_name' => $form_state->getValue('employee_name'))));
}
}