Show Children of 1st Taxonomy Term only

Events happening in the community are now at Drupal community events on www.drupal.org.
criscom's picture

I have a taxonomy vocabulary called: Search. This vocabulary is structured like this:


Partent Term 1

- Child Term 1/A

- Child Term 1/B

- Child Term 1/C

- Child Term 1/D

- Child Term 1/E

Partent Term 2

- Child Term 2/A

- Child Term 2/B

- Child Term 2/C

- Child Term 2/D

- Child Term 2/E

Partent Term 3

- Child Term 3/A

- Child Term 3/B

- Child Term 3/C

- Child Term 3/D

- Child Term 3/E

In the Content Type I can select from this vocabulary. In Display Suite Full Content View I want only the Children Terms of Parent Term 1 to show up. Their Parent Term should not be visible neither should be Parent Term 2 with its children and Parent Term 3 with its children.

The full content view of the node should look like this:

Title
Child Term 1/A, Child Term 1/B, etc.
Body Text

Can this be achieved with Display Suite or do I need a custom module for this?

Thanks,
Chris

Comments

Views

Lars Bo Jensen's picture

I suppose, you need Views for this.

full content

criscom's picture

I need this on a full content node view. Can I hook in there with views?

Add a dynamic field in Display Suite

Lars Bo Jensen's picture

Yes. I find your case a little complicated and am not sure if the view will work, but you can get it into your Display Suite node display. At /admin/structure/types/manage/your-content-type/display, under the tab Add custom fields, 'Add a dynamic field'. Give it some name, place it, select content > your view, which you should create at first:

Create your view. It's tricky, but I think that:

  • It should display taxonomy terms
  • display fields
  • have a relation: taxonomy terms: content using [your taxonomy]
  • have these contextual filters (and these can't be set before the relation):
    -- Content: NID (get from URL)
    -- Content: has taxonomy term ID with depth modifier
  • most probably have some filter criteria

And then some... good luck.

will try

criscom's picture

Thanks a lot, Lars, I will try that. Also the custom field formatter sounds like a good approach, but I am just beginning to get into PHP.

Question

Lars Bo Jensen's picture

Btw, are you certain that you shouldn't just have 3 taxonomies rather than one with parents and children?

you are right

criscom's picture

It would have been the right way to do it. But when we implemented the taxonomy tree we thought we would only use it for solr search. But then the customer changed his mind and wants to show them on nodes as well. There is so much content in there that it would be a lot of work to change the taxonomy structure.

Haven't had a need to do this

greta_drupal's picture

Haven't had a need to do this is DS, nor do I know if it is possible (such as through DS fields). So, unless you hear otherwise from DS...you might have a look at the Custom Formatter module. If you know PHP, the world is your oyster, here. Else, on that module page there is a link to some freely offered snippets from others. Some very cool stuff.

Also, try using Rendered Taxonomy Term format, in basic DS features. See if that gets you any closer.

Sorry, but I don't have any parent+child taxonomies to try for you.

Please do report back, if you find a solution. I can foresee needing something like this on a site that I am just starting to build.

You probably could literally

greta_drupal's picture

You probably could literally hide the parent using CSS, as a last resort. (Having it might even help your SEO.) If you have a live URL to a sample page, I'd be happy to take a look.

You also could probably _not_render it through a custom template, for node. Perhaps even a theme function.

I checked CSS. This is an

criscom's picture

I checked CSS. This is an option when I'd work with the Token view. The taxonomy terms, in this case, are links to the taxonomy pages, which we do not use (and have not styled). We could, of course, hide the links by CSS but would need Javascript to deactivate the links. Not an elegant solution but a way to go if I cannot get custom field formatter or a theme function work.

I cannot send you a link because the site is already online and I cannot put the taxonomy terms on.

Thanks for helping me out on this. Very much appreciated.

You wouldn't hide ALL the

greta_drupal's picture

You wouldn't hide ALL the terms, just the parent...if there is a way to get at those alone, in CSS.

Not sure if I am misunderstanding, but you can simply select the core "plain text" format (on display mode) to have the terms NOT link to taxonomy term page.

html

criscom's picture

unfortunately the HTML code doesn't allow for that:

<div class="field-items"><div class="field-item even">Type</div><div class="field-item odd">Reports &amp; Whitepapers</div><div class="field-item even">Topic</div><div class="field-item odd">Boards/Governance</div><div class="field-item even">Leadership</div></div>

The Parent terms are

  • Type
  • Topic>
  • I only need the child term from Type, which is Reports & Whitepapers.

    No way to hide this. And there could also be up to three child terms for Type.

    CSS selector (e.g., pattern matching)

    greta_drupal's picture

    Yikes. This is bad taxonomy design. So, both of those are of the same vocabulary.

    First of all, having all of the terms be wrapped in DIVs is horrible markup. You could create a custom node template where the terms more appropriately display as a UL (you can style them inline). There would be a separate UL for each vocabulary.

    You can add custom classes through DS. At least give that taxonomy field a more unique class name.

    Then you should be able to get at those parent terms through use of CSS selector (e.g., pattern matching) to hide the parent. It might only be supported by the more compliant browsers, but worth a shot.

    You also could likely

    greta_drupal's picture

    You also could likely eliminate the parent with a custom theme function (e.g., preprocess function). At least if it is okay to hide ALL parent terms.

    I am not so good with PHP coding, but I know someone who might be able to help. He just created a theme function for me to use with taxonomy (different scenario) on node pages -- so it might even be along similar lines. He is a very nice guy and very helpful; but I always like to pay him something for his help, since he is a professional web developer. If that interests you, I'll give him your contact info -- you could skype him with what you want.

    i am also not good at PHP and

    criscom's picture

    i am also not good at PHP and I always prefer somebody who knows what he's doing. of course, I am prepared to compensate.
    I still don't have the customer's final approval for this since I informed them that it will take some extra coding and time. If the customer decides that we should implement, I would like to fall back to your friend to do this. I will of course pay him for his efforts (if my budget allows for it). Thanks, Greta!

    example: theme function for taxonomy

    greta_drupal's picture

    So, for example: I have added the below function to my (subtheme) template.php file to make taxonomy terms (from taxonomy term reference fields) display as list items (UL / LI) rather than DIVs. It also adds a delimeter -- in my case a pipe seperator -- between the items.

    function o_thct_field__taxonomy_term_reference($variables) {
      $output = '';

      // Render the label, if it's not hidden.
      if (!$variables['label_hidden']) {
        $output .= '<div class="field-label"' . $variables['title_attributes'] . '>' . $variables['label'] . ':&nbsp;</div>';
      }

      // Render the items.
      $output .= '<ul class="field-items"' . $variables['content_attributes'] . '>';
      foreach ($variables['items'] as $delta => $item) {

        // Set a delimiter
        $delimiter = ' | ';
      
        // If the item is the last in the array remove the delimeter
        if (end($variables['items']) === $item) {
          $delimiter = '';
        }

        $classes = 'field-item ' . ($delta % 2 ? 'odd' : 'even');
      
        // Output each taxonomy term item, with the delimiter on the end
        $output .= '<li class="' . $classes . '"' . $variables['item_attributes'][$delta] . '>' . drupal_render($item) . $delimiter . '</li>';
      }

    Wow, thanks for posting that

    criscom's picture

    Wow, thanks for posting that code!
    I tried your code. Copy/pasted it into my theme's template.php file and changed the first line to

    function field_search_category__taxonomy_term_reference($variables) {

    field_search_category is the Name of the Term Reference field that pulls in the Taxonomy tree.

    Still, I didn't work, nothing has changed. I am really bad in this, it seems, very embarrassing. Any idea, why it didn't output my taxonomy terms as links? Maybe because I run them through Display Suite.?

    That function is only going

    greta_drupal's picture

    That function is only going to rewrite your taxonomy terms HTML as UL/LI rather than DIV. (And, add a separator character between terms.) Whether or not the term is a link depends on the field format that you use.

    1. This is the Full Content build mode (not customized through Display Suite) for my custom content type.
      Notice my selection of "link" as format. By default behavior, when a user clicks one of those taxonomy terms, it will load the taxonomy term page that will show them all nodes that contain that term.

    Only local images are allowed.


    1. This is how that looks on the processed page.
      Notice the terms are linear (rather than stacked) and have a "|" separator because of that function. [The + sign is added through CSS.]

    Only local images are allowed.


    1. This is my Teaser, customized in Display Suite, and displayed via a View (using the Display Suite view format).

    Only local images are allowed.


    Hope that clarifies a bit.

    Can you upload a screenshot

    greta_drupal's picture

    Can you upload a screenshot of your vocabulary (/structure)? And, of your DS build mode?

    Feel free to skype me if you want help trying to work this out. (Can skype from http://MultimediaByTyme.com)

    here is the taxonomy tree:

    thanks for the images. yes,

    criscom's picture

    thanks for the images. yes, this looks very good. good job :-)
    i implemented your code and changed it to the name of my theme. but it doesn't work.

    here's how i set it up in manage display: https://skitch.com/criscom/emga1/manage-display

    and here you can see how it looks on the page - still all in divs: https://skitch.com/criscom/emg2n/node-view

    still waiting for the customer's decision. i really hope he refrains from implementing it ;-)

    You have never confirmed

    greta_drupal's picture

    You have never confirmed whether this is D6 or D7. The theme API has changed for D7. I gave you code for D7. If you are using D7, doublecheck your template.php code and be sure you have cleared your cache.

    In any case:
    If your parent term is always the first one, this might work. At least test this, just for fun. In your DS build mode, next to the field format, there is a # sign. That is an offset. Means you could start display items starting with a particular one. Try enter 1 in that box. (Do not include the # sign itself.) Save and see what you get.

    Sorry, yes, I am using

    criscom's picture

    Sorry, yes, I am using D7.

    When I enter 1, the first Taxonomy term (Parent Term) "Type" is shown. I'd rather THIS one be not shown LOL.

    Oops. Sorry, I explained that

    greta_drupal's picture

    Oops. Sorry, I explained that completely backwards...and I even use it! Be nice if you could enter a negative condition, huh. Then it would work.

    I did try that: entering -1

    criscom's picture

    I did try that: entering -1 in display suite. In this case all taxonomy terms are displayed. Nevermind, Greta, for the time being it is not urgent to press this. Could I come back to you or your friend when I really have to do it? Thanks a lot for your help. I very much appreciated it.

    BEST SOLUTION : For future reference

    greta_drupal's picture

    Up to you. But, of all the potential workarounds -- given the screenshots that you have presented -- I would say the best workaround would be the template.php function that I posted to

    1. Rewrite the HTML to unstack the terms (looks horrible and wastes valuable real estate), and display in linear fashion;

    2. Add the term-id variable ($term->tid) to the list of $classes.

    3. Simply use CSS to hide the specific terms. That way, all of your parents won't be affected.

    Copy paste (use the </code>

    greta_drupal's picture

    Copy paste (use the

    </code> tags) the precise function code that you put in your template.php file.  You definitely uploaded the template.php to the correct folder -- the root folder for the theme you are using?

    This solution should definitely work:  If you added a class, to the line below,

    <code>   
    $classes = 'field-item-taxonomy ' . ($delta % 2 ? 'odd' : 'even');

    that was automatic generation of the term-id, you then could just use CSS to hide that particular term id. Unfortunately, I'm not quite sure how to write the PHP in that function; not sure if the term id is exposed. I tried replacing the $delta with term id variable, but it didn't work.

    Try this:

    1. Remove that theme function from your template.php file, and re-upload that file.

    2. Create a taxonomy-term.tpl.php file by copy/paste the below code. Upload that to your current theme template folder (if you use one) or just the root.

    3. Clear your theme registry. (Clear all caches if you aren't sure that you have cleared your theme registry.)

    Now, you should see a class name for the term-id.

    ok, will try this. thanks. I

    criscom's picture

    ok, will try this. thanks. I will get back to you tomorrow on this. it's turning 8 p.m. gotta go. thanks for your patience and your help, you are incredible. you'll hear from me tomorrow. :-)

    this sounds like a very very

    criscom's picture

    this sounds like a very very good approach. i could try that, but you wrote that you know somebody who could help. this sounds even better.

    His name is Philip. He is the

    greta_drupal's picture

    His name is Philip. He is the developer of Dynamic Display Block module -- an awesome slideshow module. You could contact him directly via his contact page http://drupal.org/user/155138/contact.

    A custom solution might take only a couple hours. No matter, his rate is very (too!) reasonable, and I am sure it will be a lot less expensive that rebuilding the taxonomy -- from what you have said.

    You could tell him Tyme sent you.

    Also, have a look at this

    greta_drupal's picture

    Also, have a look at this thread:
    http://coolestguyplanettech.com/downtown/theme-specific-drupal-taxonomy-...

    I have been assuming that you are using Drupal 7.

    Another question. Are you terms part of a taxonomy term reference field? If so, there is a template just for that. I can attach one.

    Template file workaround

    greta_drupal's picture

    Continuing on this possible workaround, of using a custom template file to hide those terms:

    You could create a custom template for the specific term-id(s), and set them to display:none.

    Or, perhaps, just add a IF/ELSE statement to the general taxonomy term template file to do that. So, something like if term-id is NOT xxx or xxx, carry on with the code. You'll have to work the actual PHP code out, since I'm shakey on the PHP syntax.

    Since I cannot attach files here, I just direct you to: http://api.drupal.org/api/drupal/modules!taxonomy!taxonomy-term.tpl.php/7

    But, here is the code for a taxonomy-term.tpl.php file.

    <?php

    /**
    * @file
    * Default theme implementation to display a term.
    *
    * Available variables:
    * - $name: the (sanitized) name of the term.
    * - $content: An array of items for the content of the term (fields and
    *   description). Use render($content) to print them all, or print a subset
    *   such as render($content['field_example']). Use
    *   hide($content['field_example']) to temporarily suppress the printing of a
    *   given element.
    * - $term_url: Direct url of the current term.
    * - $term_name: Name of the current term.
    * - $classes: String of classes that can be used to style contextually through
    *   CSS. It can be manipulated through the variable $classes_array from
    *   preprocess functions. The default values can be one or more of the following:
    *   - taxonomy-term: The current template type, i.e., "theming hook".
    *   - vocabulary-[vocabulary-name]: The vocabulary to which the term belongs to.
    *     For example, if the term is a "Tag" it would result in "vocabulary-tag".
    *
    * Other variables:
    * - $term: Full term object. Contains data that may not be safe.
    * - $view_mode: View mode, e.g. 'full', 'teaser'...
    * - $page: Flag for the full page state.
    * - $classes_array: Array of html class attribute values. It is flattened
    *   into a string within the variable $classes.
    * - $zebra: Outputs either "even" or "odd". Useful for zebra striping in
    *   teaser listings.
    * - $id: Position of the term. Increments each time it's output.
    * - $is_front: Flags true when presented in the front page.
    * - $logged_in: Flags true when the current user is a logged-in member.
    * - $is_admin: Flags true when the current user is an administrator.
    *
    * @see template_preprocess()
    * @see template_preprocess_taxonomy_term()
    * @see template_process()
    */
    ?>

    <div id="taxonomy-term-<?php print $term->tid; ?>" class="<?php print $classes; ?>">

      <?php if (!$page): ?>
        <h2><a href="<?php print $term_url; ?>"><?php print $term_name; ?></a></h2>
      <?php endif; ?>

      <div class="content taxonomy-term">
        <?php print render($content); ?>
      </div>

    </div>

    P.S. -- This group post just

    greta_drupal's picture

    P.S. -- This group post just came through, from the Taxonomy group. You might watch this thread - or others, on that group:

    http://groups.drupal.org/node/246713

    function

    greta_drupal's picture

    function o_thct_field__taxonomy_term_reference($variables)

    Sorry, I should have mentioned:

    "o_thct" is the name of my theme. replace that with the name of your theme/subtheme. Leave the rest of the function name as is, and give that a try. Might still need some adjusting, especially to target the parent term, but you can see if it affects your theme.

    Also, don't forget to clear your theme registry at least (or simply ALL caches). Any time you change template files, you need to clear the theme cache.

    By the way, you mentioned before about not having a live site to demonstrate your predicament. You REALLY should have a clone site as a testing (development) site. Always.

    Views

    jonhattan's picture

    You can find here a howto for creating such a view that shows the parent terms:

    http://cambrico.net/drupal/mostrar-la-categoria-padre-de-una-taxonomia-j...

    It's in spanish and drupal 6. The screenshots are in english and the concept is applicable to views 3 on drupal 7.

    Sorry I misunderstood the OP.

    Display Suite

    Group organizers

    Group notifications

    This group offers an RSS feed. Or subscribe to these personalized, sitewide feeds: