/** * {@inheritdoc} */ public function process($text, $langcode) { $result = new FilterProcessResult($text); // Track if widget has been found so that we can attached the // jquery_ui_filter library and settings. $has_widget = FALSE; foreach (self::$widgets as $name => $widget) { if (strpos($text, '[' . $name) === FALSE) { continue; } $has_widget = TRUE; // Remove block tags around tokens. $text = preg_replace('#<(p|div)[^>]*>\\s*(\\[/?' . $name . '[^]]*\\])\\s*</\\1>#', '\\2', $text); // Convert opening [token] to opening <div data-ui-*> tag. $text = preg_replace_callback('#\\[' . $name . '([^]]*)?\\]#is', function ($match) use($name) { // Set data-ui-* attributes from role and options. $attributes = new Attribute(['data-ui-role' => $name]); $options = $this->parseOptions($match[1]); foreach ($options as $name => $value) { $attributes->setAttribute('data-ui-' . $name, $value); } return "<div{$attributes}>"; }, $text); // Convert closing [/token] to closing </div> tag. $text = str_replace('[/' . $name . ']', '</div>', $text); } if ($has_widget) { $result->setAttachments(['library' => ['jquery_ui_filter/jquery_ui_filter'], 'drupalSettings' => ['jquery_ui_filter' => \Drupal::config('jquery_ui_filter.settings')->get()]]); } return $result->setProcessedText($text); }
/** * {@inheritdoc} */ public function viewElements(FieldItemListInterface $items, $langcode = NULL) { $output = []; $attributes = new Attribute(); if ($this->getSetting('tag') == 'h1') { $attributes->addClass('title'); $attributes->addClass('replaced-title'); $attributes->setAttribute('id', 'page-title'); } $parent = $items->getParent()->getValue(); foreach ($items as $item) { $text = $item->getValue()['value']; if ($this->getSetting('linked')) { $text = $this->l($text, $parent->urlInfo()); } $output[] = $build['string'] = ['#type' => 'inline_template', '#template' => '<{{ tag }} {{ attributes }}>{{ text }}</{{ tag }}>', '#context' => ['text' => $text, 'tag' => $this->getSetting('tag'), 'attributes' => (string) $attributes]]; } return $output; }
/** * Tests setting attributes. * @covers ::setAttribute */ public function testSetAttribute() { $attribute = new Attribute(); // Test adding various attributes. $attributes = ['alt', 'id', 'src', 'title', 'value']; foreach ($attributes as $key) { foreach (['kitten', ''] as $value) { $attribute = new Attribute(); $attribute->setAttribute($key, $value); $this->assertEquals($value, $attribute[$key]); } } // Test adding array to class. $attribute = new Attribute(); $attribute->setAttribute('class', ['kitten', 'cat']); $this->assertArrayEquals(['kitten', 'cat'], $attribute['class']->value()); // Test adding boolean attributes. $attribute = new Attribute(); $attribute['checked'] = TRUE; $this->assertTrue($attribute['checked']->value()); }