コード例 #1
0
 /**
  * {@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;
 }
コード例 #2
0
 /**
  * Tests adding class attributes with the AttributeArray helper method.
  * @covers ::addClass()
  */
 public function testAddClasses()
 {
     // Add empty Attribute object with no classes.
     $attribute = new Attribute();
     // Add no class on empty attribute.
     $attribute->addClass();
     $this->assertEmpty($attribute['class']);
     // Test various permutations of adding values to empty Attribute objects.
     foreach (array(NULL, FALSE, '', []) as $value) {
         // Single value.
         $attribute->addClass($value);
         $this->assertEmpty((string) $attribute);
         // Multiple values.
         $attribute->addClass($value, $value);
         $this->assertEmpty((string) $attribute);
         // Single value in array.
         $attribute->addClass([$value]);
         $this->assertEmpty((string) $attribute);
         // Single value in arrays.
         $attribute->addClass([$value], [$value]);
         $this->assertEmpty((string) $attribute);
     }
     // Add one class on empty attribute.
     $attribute->addClass('banana');
     $this->assertArrayEquals(array('banana'), $attribute['class']->value());
     // Add one class.
     $attribute->addClass('aa');
     $this->assertArrayEquals(array('banana', 'aa'), $attribute['class']->value());
     // Add multiple classes.
     $attribute->addClass('xx', 'yy');
     $this->assertArrayEquals(array('banana', 'aa', 'xx', 'yy'), $attribute['class']->value());
     // Add an array of classes.
     $attribute->addClass(array('red', 'green', 'blue'));
     $this->assertArrayEquals(array('banana', 'aa', 'xx', 'yy', 'red', 'green', 'blue'), $attribute['class']->value());
     // Add an array of duplicate classes.
     $attribute->addClass(array('red', 'green', 'blue'), array('aa', 'aa', 'banana'), 'yy');
     $this->assertEquals('banana aa xx yy red green blue', (string) $attribute['class']);
 }
コード例 #3
0
 /**
  * Render the taxonomy tree.
  *
  * @param string $vid
  *   Vocabulary id.
  * @param string $name
  *   An optional name for the tree. (Default: NULL).
  * @param string $description
  *   $description An optional description of the tree. (Default: NULL).
  *
  * @return string
  *   A string representing a rendered tree.
  */
 public function getTaxonomyTree($vid, $name = NULL, $description = NULL)
 {
     $output = '';
     $options = array();
     $attributes = new Attribute();
     $config = \Drupal::config('sitemap.settings');
     if (\Drupal::service('module_handler')->moduleExists('forum') && $vid == \Drupal::config('forum.settings')->get('vocabulary')) {
         $title = \Drupal::l($name, Url::fromRoute('forum.index'));
         $threshold = $config->get('forum_threshold');
         $forum_link = TRUE;
     } else {
         $title = $name;
         $threshold = $config->get('term_threshold');
         $forum_link = FALSE;
     }
     $last_depth = -1;
     $output .= !empty($description) && $config->get('show_description') ? '<div class="description">' . Xss::filterAdmin($description) . "</div>\n" : '';
     $depth = $config->get('vocabulary_depth');
     if ($depth <= -1) {
         $depth = NULL;
     }
     $tree = \Drupal::entityManager()->getStorage('taxonomy_term')->loadTree($vid, 0, $depth);
     foreach ($tree as $term) {
         $term->count = sitemap_taxonomy_term_count_nodes($term->tid);
         if ($term->count <= $threshold) {
             continue;
         }
         // Adjust the depth of the <ul> based on the change
         // in $term->depth since the $last_depth.
         if ($term->depth > $last_depth) {
             for ($i = 0; $i < $term->depth - $last_depth; $i++) {
                 $output .= "\n<ul>";
             }
         } elseif ($term->depth == $last_depth) {
             $output .= '</li>';
         } elseif ($term->depth < $last_depth) {
             for ($i = 0; $i < $last_depth - $term->depth; $i++) {
                 $output .= "</li>\n</ul>\n</li>";
             }
         }
         // Display the $term.
         $output .= "\n<li>";
         $term_item = '';
         if ($forum_link) {
             $term_item .= \Drupal::l($term->name, Url::fromRoute('forum.page', array('taxonomy_term' => $term->tid), array('attributes' => array('title' => $term->description__value))));
         } elseif ($term->count) {
             $term_item .= \Drupal::l($term->name, Url::fromRoute('entity.taxonomy_term.canonical', array('taxonomy_term' => $term->tid), array('attributes' => array('title' => $term->description__value))));
         } else {
             $term_item .= $term->name;
         }
         if ($config->get('show_count')) {
             $span_title = \Drupal::translation()->formatPlural($term->count, '1 item has this term', '@count items have this term');
             $term_item .= " <span title=\"" . $span_title . "\">(" . $term->count . ")</span>";
         }
         // RSS depth.
         $rss_depth = $config->get('rss_taxonomy');
         if ($config->get('show_rss_links') != 0 && ($rss_depth == -1 || $term->depth < $rss_depth)) {
             $feed_icon = array('#theme' => 'sitemap_feed_icon', '#url' => 'taxonomy/term/' . $term->tid . '/feed', '#name' => $term->name);
             $rss_link = drupal_render($feed_icon);
             if ($config->get('show_rss_links') == 1) {
                 $term_item .= ' ' . $rss_link;
             } else {
                 $attributes->addClass('sitemap-rss-left');
                 $term_item = $rss_link . ' ' . $term_item;
             }
         }
         // Add an alter hook for modules to manipulate the taxonomy term output.
         \Drupal::moduleHandler()->alter(array('sitemap_taxonomy_term', 'sitemap_taxonomy_term_' . $term->tid), $term_item, $term);
         $output .= $term_item;
         // Reset $last_depth in preparation for the next $term.
         $last_depth = $term->depth;
     }
     // Bring the depth back to where it began, -1.
     if ($last_depth > -1) {
         for ($i = 0; $i < $last_depth + 1; $i++) {
             $output .= "</li>\n</ul>\n";
         }
     }
     $this->setOption($options, 'show_titles', 1, 'show_titles', TRUE);
     $attributes->addClass('sitemap-box-terms', 'sitemap-box-terms-' . $vid);
     $sitemap_box = array('title' => $title, 'content' => array('#markup' => $output), 'attributes' => $attributes, 'options' => $options);
     return $sitemap_box;
 }
コード例 #4
0
ファイル: AttributeTest.php プロジェクト: papillon-cendre/d8
 /**
  * Tests checking for class names with the Attribute method.
  * @covers ::hasClass
  */
 public function testHasClass()
 {
     // Test an attribute without any classes.
     $attribute = new Attribute();
     $this->assertFalse($attribute->hasClass('a-class-nowhere-to-be-found'));
     // Add a class to check for.
     $attribute->addClass('we-totally-have-this-class');
     // Check that this class exists.
     $this->assertTrue($attribute->hasClass('we-totally-have-this-class'));
 }