diff --git a/core/modules/help/help.tokens.inc b/core/modules/help/help.tokens.inc index e69de29..4cb2751 100644 --- a/core/modules/help/help.tokens.inc +++ b/core/modules/help/help.tokens.inc @@ -0,0 +1,53 @@ + t("Help Topics"), + 'description' => t("Provides tokens for help topics."), + ); + + $topics = array(); + /* @var \Drupal\help\HelpTopicInterface */ + foreach (HelpTopic::loadMultiple() as $help_topic_id => $help_topic) { + $topics[$help_topic_id] = array( + 'name' => $help_topic->label(), + 'description' => t('URL to the @label help topic', ['@label' => $help_topic->label()]), + ); + } + + return array( + 'types' => $types, + 'tokens' => array( + 'help_topic' => $topics, + ), + ); +} + +/** + * Implements hook_tokens(). + */ +function help_tokens($type, $tokens, array $data = array(), array $options = array()) { + $replacements = array(); + + if ($type == 'help_topic') { + foreach ($tokens as $name => $original) { + if ($topic = HelpTopic::load($name)) { + $replacements[$original] = $topic->url('canonical'); + } + } + } + + return $replacements; +} diff --git a/core/modules/help/src/Controller/HelpController.php b/core/modules/help/src/Controller/HelpController.php index 3f835dd..8a1308d 100644 --- a/core/modules/help/src/Controller/HelpController.php +++ b/core/modules/help/src/Controller/HelpController.php @@ -14,7 +14,7 @@ use Drupal\Core\Utility\Token; use Drupal\Component\Utility\String; use Drupal\help\Entity\HelpTopic; -use Drupal\help\HelpInterface; +use Drupal\help\HelpTopicInterface; use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; @@ -214,13 +214,13 @@ public function helpPage($name) { /** * Renders a help topic page from a configured help entity. * - * @param \Drupal\help\HelpInterface $help_topic + * @param \Drupal\help\HelpTopicInterface $help_topic * The help entity to display. * * @return array * A render array for the help topic page. */ - public function helpEntityView(HelpInterface $help_topic) { + public function helpEntityView(HelpTopicInterface $help_topic) { $build = array(); $build['#title'] = String::checkPlain($help_topic->get('label')); diff --git a/core/modules/help/src/Entity/HelpTopic.php b/core/modules/help/src/Entity/HelpTopic.php index 27482c3..d56725f 100644 --- a/core/modules/help/src/Entity/HelpTopic.php +++ b/core/modules/help/src/Entity/HelpTopic.php @@ -8,7 +8,7 @@ namespace Drupal\help\Entity; use Drupal\Core\Config\Entity\ConfigEntityBase; -use Drupal\help\HelpInterface; +use Drupal\help\HelpTopicInterface; /** * Defines a configuration entity for help topics. @@ -55,7 +55,7 @@ * } * ) */ -class HelpTopic extends ConfigEntityBase implements HelpInterface { +class HelpTopic extends ConfigEntityBase implements HelpTopicInterface { /** * The topic machine name. * diff --git a/core/modules/help/src/Tests/HelpTopicTokensTest.php b/core/modules/help/src/Tests/HelpTopicTokensTest.php index e69de29..95a2405 100644 --- a/core/modules/help/src/Tests/HelpTopicTokensTest.php +++ b/core/modules/help/src/Tests/HelpTopicTokensTest.php @@ -0,0 +1,53 @@ +installSchema('system', array('router', 'sequences')); + $this->installConfig(array('help')); + \Drupal::service('router.builder')->rebuild(); + + } + + /** + * Tests that help topic tokens work. + */ + public function testHelpTopicTokens() { + $text = 'This should Link to help topic'; + $replaced = \Drupal::token()->replace($text); + $this->assertTrue(strpos($replaced, 'replace($text); + $this->assertTrue(strpos($replaced, '[help_topic:nonexistant]') !== FALSE); + } + +}