Last active
April 21, 2017 13:00
-
-
Save daggerhart/96f2d8b878d470cdf9c924273fe9749c to your computer and use it in GitHub Desktop.
WordPress Widget API vs Drupal 8 Block Plugin API
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\example_block\Plugin\Block; | |
use Drupal\Core\Block\BlockBase; | |
use Drupal\Core\Block\BlockPluginInterface; | |
use Drupal\Core\Form\FormStateInterface; | |
/** | |
* Provides a Eventbrite attendees list Block. | |
* | |
* @Block( | |
* id = "example_block", | |
* admin_label = @Translation("My example block") | |
* ) | |
*/ | |
class ExampleBlock extends BlockBase implements BlockPluginInterface | |
{ | |
/** | |
* Return the block contents | |
* | |
* {@inheritdoc} | |
*/ | |
public function build() | |
{ | |
$config = $this->getConfiguration(); | |
return [ | |
'#markup' => "Hello {$config['some_text']}" | |
]; | |
} | |
/** | |
* Return a Form API array | |
* | |
* {@inheritdoc} | |
*/ | |
public function blockForm($form, FormStateInterface $form_state) | |
{ | |
$form = parent::blockForm($form, $form_state); | |
$config = $this->getConfiguration(); | |
$form['some_text'] = [ | |
'#type' => 'textfield', | |
'#title' => $this->t('Some Text'), | |
'#default_value' => isset($config['some_text']) ? $config['some_text'] : 'Jonathan', | |
]; | |
return $form; | |
} | |
/** | |
* Update the configuration values from the form state | |
* | |
* {@inheritdoc} | |
*/ | |
public function blockSubmit($form, FormStateInterface $form_state) | |
{ | |
$this->configuration['some_text'] = $form_state->getValue('some_text'); | |
} | |
} | |
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 | |
class ExampleWidget extends WP_Widget { | |
/** | |
* ExampleWidget constructor. | |
*/ | |
function __construct() { | |
parent::__construct( | |
// id_base | |
'example_widget_id', | |
// title | |
__( 'My example widget' ), | |
// widget options | |
array( | |
'classname' => 'examplewidget', | |
'description' => __( 'My example is great!' ) | |
) | |
); | |
} | |
/** | |
* Echo the widget contents to the screen | |
* | |
* @param array $args | |
* @param array $instance | |
*/ | |
function widget( $args, $instance ) { | |
print "Hello {$instance['some_text']}"; | |
} | |
/** | |
* Echo the widget form to the screen | |
* | |
* @param array $instance | |
*/ | |
function form( $instance ) { | |
$default_values = array( | |
'some_text' => 'Jonathan', | |
); | |
$instance = wp_parse_args( $instance, $default_values ); | |
?> | |
<label for="<?php print $this->get_field_id( 'some_text' ) ?>"><?php _e('Some Text') ?></label> | |
<input type="text" | |
id="<?php print $this->get_field_id( 'some_text' ) ?>" | |
name="<?php print $this->get_field_name( 'some_text' ) ?>" | |
value="<?php print esc_html( $instance['some_text'] ) ?>" /> | |
<?php | |
} | |
/** | |
* Sanitize a new instance array and return it | |
* | |
* @param array $new_instance | |
* @param array $old_instance | |
*/ | |
function update( $new_instance, $old_instance ) { | |
$instance['some_text'] = sanitize_text_field( $new_instance['some_text'] ); | |
return $instance; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment