Skip to content

Instantly share code, notes, and snippets.

@sdboyer
Last active August 29, 2015 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sdboyer/9879940 to your computer and use it in GitHub Desktop.
Save sdboyer/9879940 to your computer and use it in GitHub Desktop.
<?php
class BlockNGInterface extends HtmlFragmentInterface, ConfigurablePluginInterface, PluginFormInterface, PluginInspectionInterface /*, CacheableInterface */ {
public function access(AccountInterface $account);
public function getMachineNameSuggestion();
// public function build(); // DIE DIE DIE
}
<?php
/**
* Trait for blocks that are dependent on render arrays.
*
* AKA, "The Block Demultiplexer"
*/
trait RenderArrayBasedBlock /* implements BlockNGInterface */ {
// Optionally override this if the block wants to have a different way
// of getting the title
public function getTitle() {
$r = $this->render();
return $r['#title'];
}
public function getContent() {
$r = $this->render();
return $r['#content'];
}
public function getMeta() {
$r = $this->render();
// TODO this isn't so well-established right now
return $r['#meta'];
}
public function getAssets() {
// a version of drupal_process_attached() that returns found assets instead
// of adding them to the global static caches
return drupal_collect_attached($this->render());
}
// Ensure we the render array is built only once
final protected function render() {
if (!isset($this->render)) {
$this->render = $this->build();
}
return $this->render;
}
abstract protected function build();
}
<?php
interface HtmlFragmentInterface {
public function getTitle();
public function getContent();
public function getMeta();
public function getAssets();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment