Version 1
[yaffs-website] / web / core / modules / tour / src / Plugin / tour / tip / TipPluginText.php
1 <?php
2
3 namespace Drupal\tour\Plugin\tour\tip;
4
5 use Drupal\Component\Utility\Html;
6 use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
7 use Drupal\Core\Utility\Token;
8 use Drupal\tour\TipPluginBase;
9 use Symfony\Component\DependencyInjection\ContainerInterface;
10
11 /**
12  * Displays some text as a tip.
13  *
14  * @Tip(
15  *   id = "text",
16  *   title = @Translation("Text")
17  * )
18  */
19 class TipPluginText extends TipPluginBase implements ContainerFactoryPluginInterface {
20
21   /**
22    * The body text which is used for render of this Text Tip.
23    *
24    * @var string
25    */
26   protected $body;
27
28   /**
29    * Token service.
30    *
31    * @var \Drupal\Core\Utility\Token
32    */
33   protected $token;
34
35   /**
36    * The forced position of where the tip will be located.
37    *
38    * @var string
39    */
40   protected $location;
41
42   /**
43    * Constructs a \Drupal\tour\Plugin\tour\tip\TipPluginText object.
44    *
45    * @param array $configuration
46    *   A configuration array containing information about the plugin instance.
47    * @param string $plugin_id
48    *   The plugin_id for the plugin instance.
49    * @param mixed $plugin_definition
50    *   The plugin implementation definition.
51    * @param \Drupal\Core\Utility\Token $token
52    *   The token service.
53    */
54   public function __construct(array $configuration, $plugin_id, $plugin_definition, Token $token) {
55     parent::__construct($configuration, $plugin_id, $plugin_definition);
56     $this->token = $token;
57   }
58
59   /**
60    * {@inheritdoc}
61    */
62   public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
63     return new static($configuration, $plugin_id, $plugin_definition, $container->get('token'));
64   }
65
66   /**
67    * Returns a ID that is guaranteed uniqueness.
68    *
69    * @return string
70    *   A unique id to be used to generate aria attributes.
71    */
72   public function getAriaId() {
73     static $id;
74     if (!isset($id)) {
75       $id = Html::getUniqueId($this->get('id'));
76     }
77     return $id;
78   }
79
80   /**
81    * Returns body of the text tip.
82    *
83    * @return string
84    *   The tip body.
85    */
86   public function getBody() {
87     return $this->get('body');
88   }
89
90   /**
91    * Returns location of the text tip.
92    *
93    * @return string
94    *   The tip location.
95    */
96   public function getLocation() {
97     return $this->get('location');
98   }
99
100   /**
101    * {@inheritdoc}
102    */
103   public function getAttributes() {
104     $attributes = parent::getAttributes();
105     $attributes['data-aria-describedby'] = 'tour-tip-' . $this->getAriaId() . '-contents';
106     $attributes['data-aria-labelledby'] = 'tour-tip-' . $this->getAriaId() . '-label';
107     if ($location = $this->get('location')) {
108       $attributes['data-options'] = 'tipLocation:' . $location;
109     }
110     return $attributes;
111   }
112
113   /**
114    * {@inheritdoc}
115    */
116   public function getOutput() {
117     $output = '<h2 class="tour-tip-label" id="tour-tip-' . $this->getAriaId() . '-label">' . Html::escape($this->getLabel()) . '</h2>';
118     $output .= '<p class="tour-tip-body" id="tour-tip-' . $this->getAriaId() . '-contents">' . $this->token->replace($this->getBody()) . '</p>';
119     return ['#markup' => $output];
120   }
121
122 }