Drupal 8:如果以管理员身份登录,则不显示自定义表单

问题描述 投票:0回答:1

在D8中,我想在自定义生成的块中显示一个自定义表单。我已经创建了所需的自定义模块,并为该块分配了一个区域。如果以管理员或经过身份验证的用户身份登录,则前端的任何区域都不会显示任何内容。但是,如果我将前端视为匿名用户,则表单即将到来。下面是我的代码。需要您的帮助。

/src/Plugin/Block/DashBlock.php

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;
  }
}

/src/Form/WorkForm.php

/**
 * @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'))));

  }
}
drupal drupal-8
1个回答
0
投票

可以在管理视图中管理块的可见性,请转到主页>管理>结构>块布局,然后选择要编辑的区域/块,然后单击配置

从那里您可以将块的可见性限制为特定的用户角色(内容类型,语言,路径也是如此)。在您的情况下,您一定要选择"Anonymous user",而不要选择它。

只需取消选择/选择要限制其访问的适当角色,然后保存。

example

© www.soinside.com 2019 - 2024. All rights reserved.