Skip to content

Instantly share code, notes, and snippets.

@danielnv18
Created January 1, 2015 02:47
Show Gist options
  • Save danielnv18/d4cfb0993ff98fda6d7e to your computer and use it in GitHub Desktop.
Save danielnv18/d4cfb0993ff98fda6d7e to your computer and use it in GitHub Desktop.
<?php
/**
* @file
* Contains Drupal\demo\Plugin\Block\DemoBlock.
*/
namespace Drupal\demo\Plugin\Block;
use Drupal\Core\Block\BlockBase;
/**
* Provides a 'DemoBlock' block.
*
* @Block(
* id = "demo_block",
* admin_label = @Translation("Demo Block")
* )
*/
class DemoBlock extends BlockBase {
/**
* {@inheritdoc}
*/
public function build() {
return [
'#markup' => 'Block demo',
];
}
}
<?php
/**
* @file
* Contains Drupal\example\Plugin\Block\DefaultBlock.
*/
namespace Drupal\example\Plugin\Block;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Form\FormStateInterface;
/**
* Provides a 'DefaultBlock' block.
*
* @Block(
* id = "default_block",
* admin_label = @Translation("default_block")
* )
*/
class DefaultBlock extends BlockBase {
/**
* {@inheritdoc}
*/
public function build() {
return [
'#markup' => 'default_block',
];
}
/**
* Overrides \Drupal\block\BlockBase::blockForm().
*/
public function blockForm($form, FormStateInterface $form_state) {
$form = parent::blockForm($form, $form_state);
$config = $this->getConfiguration();
$form['name'] = [
'#type' => 'textfield',
'#title' => $this->t('Name'),
'#description' => $this->t(''),
'#default_value' => isset($config['name'])?$config['name']:"",
];
return $form;
}
/**
* Overrides \Drupal\block\BlockBase::blockSubmit().
*/
public function blockSubmit($form, FormStateInterface $form_state) {
$this->configuration['name'] = $form_state->getValue('name');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment