2016-06-03 7 views
1

사용자 정의 블록에 표시해야하는 사용자 정의 등록 양식을 작성하려고하는데 일반 등록 양식을 삽입하고 후크를 통해 변경하거나 form_block과 같은 확장자를 사용하지 않으려 고합니다. Drupal 8이 양식과 함께 작동하는 방법을 배우고 싶습니다.Drupal 8 사용자 정의 등록 양식

내 블록은 다음과 같습니다

내 사용자 등록 양식은 다음과 같습니다
<?php 

namespace Drupal\ajax_registration_form\Plugin\Block; 

use Drupal\ajax_registration_form\Form\AjaxRegistrationForm; 
use Drupal\Core\Block\BlockBase; 

/** 
* Provides a 'AjaxRegistrationBlock' block. 
* 
* @Block(
* id = "ajax_registration_block", 
* admin_label = @Translation("Ajax registration block"), 
*) 
*/ 
class AjaxRegistrationBlock extends BlockBase { 

    /** 
    * {@inheritdoc} 
    */ 
    public function build() { 

    $content = \Drupal::formBuilder()->getForm(AjaxRegistrationForm::class); 

    return $content; 
    } 

} 

:

<?php 

namespace Drupal\ajax_registration_form\Form; 

use Drupal\Core\Form\FormStateInterface; 
use Drupal\user\RegisterForm; 



class AjaxRegistrationForm extends RegisterForm { 


    /** 
    * {@inheritdoc} 
    */ 
    public function form(array $form, FormStateInterface $form_state) { 

    return parent::form($form, $form_state); 
    } 
} 

난 그냥 일반 RegisterForm을 확장하려하고 첫 번째 단계에서 그냥 돌아 싶어 부모 양식이 작동하는지 확인하십시오. 하지만 ...하지 않는

오류 메시지 :

Fatal error: Call to a member function getEntityTypeId() on null in /Users/*******/Sites/priv/radweiser/web/core/lib/Drupal/Core/Entity/EntityForm.php on line 77 

나는 그것이 형태로 누락 된 사용자 엔티티 생각,하지만 난 내 양식이 실체를 "넣을"수있는 방법을 모른다 .

답변

0

formblock 모듈의 코드에서 해결책을 찾았습니다.

나는 이런 식으로 뭔가 내 블록을 변경 :

<?php 

namespace Drupal\ajax_registration_form\Plugin\Block; 

use Drupal\Core\Annotation\Translation; 
use Drupal\Core\Block\Annotation\Block; 
use Drupal\Core\Block\BlockBase; 
use Drupal\Core\Entity\EntityFormBuilderInterface; 
use Drupal\Core\Entity\EntityManagerInterface; 
use Drupal\Core\Plugin\ContainerFactoryPluginInterface; 
use Drupal\Core\Session\AccountInterface; 
use Symfony\Component\DependencyInjection\ContainerInterface; 

/** 
* Provides a 'AjaxRegistrationBlock' block. 
* 
* @Block(
* id = "ajax_registration_block", 
* admin_label = @Translation("Ajax registration block"), 
*) 
*/ 
class AjaxRegistrationBlock extends BlockBase implements ContainerFactoryPluginInterface { 


    /** 
    * The entity manager 
    * 
    * @var \Drupal\Core\Entity\EntityManagerInterface. 
    */ 
    protected $entityManager; 

    /** 
    * The entity form builder 
    * 
    * @var \Drupal\Core\Entity\EntityManagerInterface. 
    */ 
    protected $entityFormBuilder; 

    /** 
    * Constructs a new UserRegisterBlock plugin 
    * 
    * @param array $configuration 
    * A configuration array containing information about the plugin instance. 
    * @param string $plugin_id 
    * The plugin_id for the plugin instance. 
    * @param mixed $plugin_definition 
    * The plugin implementation definition. 
    * @param \Drupal\Core\Entity\EntityManagerInterface $entityManager 
    * The entity manager. 
    * @param \Drupal\Core\Entity\EntityFormBuilderInterface $entityFormBuilder 
    * The entity form builder. 
    */ 
    public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityManagerInterface $entityManager, EntityFormBuilderInterface $entityFormBuilder) { 
    parent::__construct($configuration, $plugin_id, $plugin_definition); 
    $this->entityManager = $entityManager; 
    $this->entityFormBuilder = $entityFormBuilder; 
    } 

    /** 
    * @inheritdoc 
    */ 
    public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { 
    return new static(
     $configuration, 
     $plugin_id, 
     $plugin_definition, 
     $container->get('entity.manager'), 
     $container->get('entity.form_builder') 
    ); 
    } 


    /** 
    * Implements \Drupal\block\BlockBase::build(). 
    */ 
    public function build() { 
    $build = array(); 

    $account = $this->entityManager->getStorage('user') ->create(array()); 
    $build['form'] = $this->entityFormBuilder->getForm($account, 'register'); 

    $build['form']['account']['mail']['#description'] = t(''); 
    kint($build['form']['account']); 

    return $build; 
    } 

    /** 
    *Implements \Drupal\block\BlockBase::blockAccess(). 
    * 
    * @param \Drupal\Core\Session\AccountInterface $account 
    * 
    * @return bool|\Drupal\Core\Access\AccessResult 
    */ 
    public function blockAccess(AccountInterface $account) { 
    return ($account->isAnonymous()) && (\Drupal::config('user.settings')->get('register') != USER_REGISTER_ADMINISTRATORS_ONLY); 
    } 

} 

가 지금은 양식을 변경하고 양식 API를 사용하여 AJAX 로직을 구현할 수

(예를 들어 메일 입력 설명을 변경)