-
-
Save munavlakshmi/9d50c1f27b80807865fd0dad14e47561 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Drupal\Defaultblock\Plugin\Block; | |
use Drupal\Core\Block\BlockBase; | |
use Drupal\Core\Form\FormStateInterface; | |
use Drupal\Core\Block\BlockPluginInterface; | |
/** | |
* Provides a 'CustomBlock' block. | |
* | |
* @Block( | |
* id = "id_custom_block", | |
* admin_label = @Translation("labelcustomblock"), | |
* ) | |
*/ | |
class DefaultBlock extends BlockBase implements BlockPluginInterface { | |
/** | |
* {@inheritdoc} | |
*/ | |
public function blockForm($form, FormStateInterface $form_state) { | |
$form = parent::blockForm($form, $form_state); | |
$config = $this->getConfiguration(); | |
$form['hello_block_name'] = array ( | |
'#type' => 'textfield', | |
'#title' => $this->t('Who'), | |
'#description' => $this->t('Who do you want to say hello to?'), | |
'#default_value' => isset($config['name']) ? $config['name'] : '', | |
); | |
$form['mobile_number'] = array ( | |
'#type' => 'textfield', | |
'#title' => $this->t('your number'), | |
'#description' => $this->t('enter your personal mobile number'), | |
'#default_value' => isset($config['number']) ? $config['number'] : '', | |
); | |
$form['address'] = array ( | |
'#type' => 'textfield', | |
'#title' => $this->t('your address'), | |
'#description' => $this->t('enter your home town'), | |
'#default_value' => isset($config['town']) ? $config['town'] : '', | |
); | |
return $form; | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function blockSubmit($form, FormStateInterface $form_state) { | |
$this->setConfigurationValue('name', $form_state->getValue('hello_block_name')); | |
$this->setConfigurationValue('number', $form_state->getValue('mobile_number')); | |
$this->setConfigurationValue('town', $form_state->getValue('address')); | |
} | |
public function build() { | |
$config = $this->getConfiguration(); | |
if (!empty($config['name'])) { | |
$name = $config['name']; | |
} | |
else { | |
$name = $this->t('to no one'); | |
} | |
if (!empty($config['number'])) { | |
$number = $config['number']; | |
} | |
else { | |
$number = $this->t('mobile number'); | |
} | |
if (!empty($config['town'])) { | |
$town = $config['town']; | |
} | |
else { | |
$town = $this->t("@town"); | |
} | |
return array( | |
'#markup' => $this->t('Hi my number is @number!,@name and my address @town', | |
array ( | |
'@number' => $number, '@name' => $name, '@town' => $town, | |
) | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment