Browse Source

Reorder functions.

8.x-1.x
Chi 8 years ago
parent
commit
2cb9390ca4
  1. 196
      src/TwigExtension.php
  2. 4
      tests/twig_tweak_test/templates/twig-tweak-test.html.twig

196
src/TwigExtension.php

@ -104,10 +104,11 @@ class TwigExtension extends \Twig_Extension {
new \Twig_SimpleFunction('drupal_view', 'views_embed_view'), new \Twig_SimpleFunction('drupal_view', 'views_embed_view'),
new \Twig_SimpleFunction('drupal_block', [$this, 'drupalBlock']), new \Twig_SimpleFunction('drupal_block', [$this, 'drupalBlock']),
new \Twig_SimpleFunction('drupal_region', [$this, 'drupalRegion']), new \Twig_SimpleFunction('drupal_region', [$this, 'drupalRegion']),
new \Twig_SimpleFunction('drupal_token', [$this, 'drupalToken']),
new \Twig_SimpleFunction('drupal_entity', [$this, 'drupalEntity']), new \Twig_SimpleFunction('drupal_entity', [$this, 'drupalEntity']),
new \Twig_SimpleFunction('drupal_field', [$this, 'drupalField']), new \Twig_SimpleFunction('drupal_field', [$this, 'drupalField']),
new \Twig_SimpleFunction('drupal_menu', [$this, 'drupalMenu']), new \Twig_SimpleFunction('drupal_menu', [$this, 'drupalMenu']),
new \Twig_SimpleFunction('drupal_form', [$this, 'drupalForm']),
new \Twig_SimpleFunction('drupal_token', [$this, 'drupalToken']),
new \Twig_SimpleFunction('drupal_config', [$this, 'drupalConfig']), new \Twig_SimpleFunction('drupal_config', [$this, 'drupalConfig']),
new \Twig_SimpleFunction('drupal_dump', [$this, 'drupalDump']), new \Twig_SimpleFunction('drupal_dump', [$this, 'drupalDump']),
new \Twig_SimpleFunction('dd', [$this, 'drupalDump']), new \Twig_SimpleFunction('dd', [$this, 'drupalDump']),
@ -140,6 +141,21 @@ class TwigExtension extends \Twig_Extension {
return 'twig_tweak'; return 'twig_tweak';
} }
/**
* Builds the render array for the provided block.
*
* @param mixed $id
* The ID of the block to render.
*
* @return null|array
* A render array for the block or NULL if the block does not exist.
*/
public function drupalBlock($id) {
$block = $this->entityTypeManager->getStorage('block')->load($id);
return $block ?
$this->entityTypeManager->getViewBuilder('block')->view($block) : '';
}
/** /**
* Builds the render array of a given region. * Builds the render array of a given region.
* *
@ -176,45 +192,6 @@ class TwigExtension extends \Twig_Extension {
return $build; return $build;
} }
/**
* Builds the render array for the provided block.
*
* @param mixed $id
* The ID of the block to render.
*
* @return null|array
* A render array for the block or NULL if the block does not exist.
*/
public function drupalBlock($id) {
$block = $this->entityTypeManager->getStorage('block')->load($id);
return $block ?
$this->entityTypeManager->getViewBuilder('block')->view($block) : '';
}
/**
* Replaces a given tokens with appropriate value.
*
* @param string $token
* A replaceable token.
* @param array $data
* (optional) An array of keyed objects. For simple replacement scenarios
* 'node', 'user', and others are common keys, with an accompanying node or
* user object being the value. Some token types, like 'site', do not
* require any explicit information from $data and can be replaced even if
* it is empty.
* @param array $options
* (optional) A keyed array of settings and flags to control the token
* replacement process.
*
* @return string
* The token value.
*
* @see \Drupal\Core\Utility\Token::replace()
*/
public function drupalToken($token, array $data = [], array $options = []) {
return $this->token->replace("[$token]", $data, $options);
}
/** /**
* Returns the render array for an entity. * Returns the render array for an entity.
* *
@ -307,6 +284,43 @@ class TwigExtension extends \Twig_Extension {
return $this->menuTree->build($tree); return $this->menuTree->build($tree);
} }
/**
* Builds and processes a form for a given form ID.
*
* @param string $form_id
* The form ID.
*
* @return array
* A render array to represent the form.
*/
public function drupalForm($form_id) {
return \Drupal::formBuilder()->getForm($form_id);
}
/**
* Replaces a given tokens with appropriate value.
*
* @param string $token
* A replaceable token.
* @param array $data
* (optional) An array of keyed objects. For simple replacement scenarios
* 'node', 'user', and others are common keys, with an accompanying node or
* user object being the value. Some token types, like 'site', do not
* require any explicit information from $data and can be replaced even if
* it is empty.
* @param array $options
* (optional) A keyed array of settings and flags to control the token
* replacement process.
*
* @return string
* The token value.
*
* @see \Drupal\Core\Utility\Token::replace()
*/
public function drupalToken($token, array $data = [], array $options = []) {
return $this->token->replace("[$token]", $data, $options);
}
/** /**
* Gets data from this configuration. * Gets data from this configuration.
* *
@ -323,22 +337,47 @@ class TwigExtension extends \Twig_Extension {
} }
/** /**
* Evaluates a string of PHP code. * Dumps information about variables.
*/
public function drupalDump() {
$var_dumper = '\Symfony\Component\VarDumper\VarDumper';
if (class_exists($var_dumper)) {
call_user_func($var_dumper . '::dump', func_get_args());
}
else {
trigger_error('Could not dump the variable because symfony/var-dumper component is not installed.', E_USER_WARNING);
}
}
/**
* An alias for self::drupalDump().
* *
* @param string $code * @see \Drupal\twig_tweak\TwigExtension::drupalDump();
* Valid PHP code to be evaluated. */
public function dd() {
$this->drupalDump(func_get_args());
}
/**
* Sets a message to display to the user.
* *
* @return mixed * @param string|\Drupal\Component\Render\MarkupInterface $message
* The eval() result. * (optional) The translated message to be displayed to the user.
* @param string $type
* (optional) The message's type. Defaults to 'status'.
* @param bool $repeat
* (optional) If this is FALSE and the message is already set, then the
* message will not be repeated. Defaults to FALSE.
*
* @return array
* A render array to disable caching.
*
* @see drupal_set_message()
*/ */
public function phpFilter($code) { public function drupalSetMessage($message = NULL, $type = 'status', $repeat = FALSE) {
ob_start(); drupal_set_message($message, $type, $repeat);
// @codingStandardsIgnoreStart $build['#cache']['max-age'] = 0;
print eval($code); return $build;
// @codingStandardsIgnoreEnd
$output = ob_get_contents();
ob_end_clean();
return $output;
} }
/** /**
@ -390,47 +429,22 @@ class TwigExtension extends \Twig_Extension {
} }
/** /**
* Sets a message to display to the user. * Evaluates a string of PHP code.
*
* @param string|\Drupal\Component\Render\MarkupInterface $message
* (optional) The translated message to be displayed to the user.
* @param string $type
* (optional) The message's type. Defaults to 'status'.
* @param bool $repeat
* (optional) If this is FALSE and the message is already set, then the
* message will not be repeated. Defaults to FALSE.
*
* @return array
* A render array to disable caching.
* *
* @see drupal_set_message() * @param string $code
*/ * Valid PHP code to be evaluated.
public function drupalSetMessage($message = NULL, $type = 'status', $repeat = FALSE) {
drupal_set_message($message, $type, $repeat);
$build['#cache']['max-age'] = 0;
return $build;
}
/**
* Dumps information about variables.
*/
public function drupalDump() {
$var_dumper = '\Symfony\Component\VarDumper\VarDumper';
if (class_exists($var_dumper)) {
call_user_func($var_dumper . '::dump', func_get_args());
}
else {
trigger_error('Could not dump the variable because symfony/var-dumper component is not installed.', E_USER_WARNING);
}
}
/**
* An alias for self::drupalDump().
* *
* @see \Drupal\twig_tweak\TwigExtension::drupalDump(); * @return mixed
* The eval() result.
*/ */
public function dd() { public function phpFilter($code) {
$this->drupalDump(func_get_args()); ob_start();
// @codingStandardsIgnoreStart
print eval($code);
// @codingStandardsIgnoreEnd
$output = ob_get_contents();
ob_end_clean();
return $output;
} }
} }

4
tests/twig_tweak_test/templates/twig-tweak-test.html.twig

@ -19,6 +19,8 @@
<div class="tt-view-default">{{ drupal_view('twig_tweak_test') }}</div> <div class="tt-view-default">{{ drupal_view('twig_tweak_test') }}</div>
<div class="tt-view-page_1">{{ drupal_view('twig_tweak_test', 'page_1') }}</div> <div class="tt-view-page_1">{{ drupal_view('twig_tweak_test', 'page_1') }}</div>
<div class="tt-view-page_1-with-argument">{{ drupal_view('twig_tweak_test', 'page_1', 1) }}</div> <div class="tt-view-page_1-with-argument">{{ drupal_view('twig_tweak_test', 'page_1', 1) }}</div>
<div class="tt-block">{{ drupal_block('powered_by_drupal') }}</div>
<div class="tt-region">{{ drupal_region('sidebar_first') }}</div>
<div class="tt-entity-default">{{ drupal_entity('node', 1) }}</div> <div class="tt-entity-default">{{ drupal_entity('node', 1) }}</div>
<div class="tt-entity-teaser">{{ drupal_entity('node', 1, 'teaser') }}</div> <div class="tt-entity-teaser">{{ drupal_entity('node', 1, 'teaser') }}</div>
<div class="tt-entity-from-url">{{ drupal_entity('node') }}</div> <div class="tt-entity-from-url">{{ drupal_entity('node') }}</div>
@ -26,8 +28,6 @@
<div class="tt-menu-default">{{ drupal_menu('twig-tweak-test') }}</div> <div class="tt-menu-default">{{ drupal_menu('twig-tweak-test') }}</div>
<div class="tt-menu-level">{{ drupal_menu('twig-tweak-test', 2) }}</div> <div class="tt-menu-level">{{ drupal_menu('twig-tweak-test', 2) }}</div>
<div class="tt-menu-depth">{{ drupal_menu('twig-tweak-test', 1, 1) }}</div> <div class="tt-menu-depth">{{ drupal_menu('twig-tweak-test', 1, 1) }}</div>
<div class="tt-region">{{ drupal_region('sidebar_first') }}</div>
<div class="tt-block">{{ drupal_block('powered_by_drupal') }}</div>
<div class="tt-token">{{ drupal_token('site:name') }}</div> <div class="tt-token">{{ drupal_token('site:name') }}</div>
<div class="tt-token-data">{{ drupal_token('node:title', {'node': node}) }}</div> <div class="tt-token-data">{{ drupal_token('node:title', {'node': node}) }}</div>
<div class="tt-config">{{ drupal_config('user.settings', 'anonymous') }}</div> <div class="tt-config">{{ drupal_config('user.settings', 'anonymous') }}</div>

Loading…
Cancel
Save