0

how to access the set block layout region from a plugin block class build method ?

class HalPublicationBlock extends BlockBase implements BlockPluginInterface
{

  /**
   * {@inheritdoc}
   */
  public function build()
  {
        //do not contain the layout region of the block
        $config = $this->getConfiguration();
        //$region=$this->get->?

EDIT : This block, being placed on /user/*, I'd like to display or not the block and allow the user to choose the region depending on his choice on some user custom fields

  public function build()
      {
        $current_path = \Drupal::service('path.current')->getPath();
        $exploded_path = explode('/', $current_path);
        $uid = end($exploded_path);
        if (prev($exploded_path) == 'user') {
          $user = User::load($uid);
          if ($user) {              
            if ($user->hasField('field_publications_bloc_region')) {
                  $block_region=$user->get('field_publications_bloc_region')->getString();
            }
           //change this block region with $block_region ( none or  content or sidebar etc)
  • You don't, the region is part of the block entity, not your configuration. Block Plugins are designed to not rely on block entities as they can also be used by Pager Manager/Panels. What are you trying to achieve? – Berdir Mar 1 '17 at 15:51
  • i have edited it – Matoeil Mar 2 '17 at 15:30
  • 1
    The region is not something you can control from within the region, that's just not possible. You need something else for that, hook_preprocess_page() possibly or subclass/replace the \Drupal\block\BlockRepository service – Berdir Mar 2 '17 at 18:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.