2

I have an activity-content-type with a taxonomy field, where I can choose from different taxonomy terms related to the activity. Now I have different views and I want to show the specific view on every activity-page, which is related to the chosen taxonomy-fields.

Lets say I have different bike-tours and diff. hikes (activities). Some are for beginners and some are for pros - some are for families and some are for couples and so on. Now I want to cross-link between the different activities and give more inspiration regarding suitable activities. If someone clicks on a bike-tour that is tagged with "beginners" and "families", drupal should show a view that offers other activities (bike-tours and hikes) that suit that criteria - also tagged with "beginners" and "families". Something like "Other familyfriendly beginner activities".

The machine names of the views don't relate with the chosen option at the moment. I would need to build a view for every combination at the moment - this is fine now (few combinations) but not scalable. I thought about combining all in one view (ideal), but couldn't find a solution yet.

Emanuel

  • 1
    What is the criteria for choosing a View ? What are the machine names of your views and how they relate with the chosen option ? Can all of those views be combined in one view ? – O V Mar 15 '17 at 19:02
  • Lets say I have different bike-tours and diff. hikes (activities). Some are for beginners and some are for pros - some are for families and some are for couples and so on. Now I want to cross-link between the different activities and give more inspiration regarding suitable activities. If someone clicks on a bike-tour that is tagged with "beginners" and "families", drupal should show a view that offers other activities (bike-tours and hikes) that suit that criteria - also tagged with "beginners" and "families". Something like "Other familyfriendly beginner activities". – tinytree Mar 16 '17 at 8:16
  • The machine names of the views don't relate with the chosen option at the moment. I would need to build a view for every combination at the moment - this is fine now (few combinations) but not scalable. I thought about combining all in one view (ideal), but couldn't find a solution yet. – tinytree Mar 16 '17 at 8:23
  • Thanks for clarifying. Would you mind posting all of this into the question , for better readability and brevity. I will post an answer for you shortly. – O V Mar 16 '17 at 11:42
0

The general idea is the following:

You create a custom block (via custom module). You place it through the interace to be visible only on nodes of type "A".

The code checks the value of field_tags.

  1. if it is "tag1" -> views_embed_view('test_view_1', 'block_1');
  2. if it is "tag2" -> views_embed_view('test_view_2', 'block_1);

You can see code below:

namespace Drupal\form_overwrite\Plugin\Block;

use Drupal\Core\Block\BlockBase;

/**
 * Provides a 'DefaultBlock' block.
 *
 * @Block(
 *  id = "default_block",
 *  admin_label = @Translation("Default block"),
 * )
 */
class DefaultBlock extends BlockBase {

  /**
   * {@inheritdoc}
   */
  public function build() {

    // Gets current node.
    $node = \Drupal::routeMatch()->getParameter('node');

    // Assuming only 1 value here.
    $tid = $node->get('field_tags')->getString();

    // If there are more values you have to loop through them. The format is.
    // $tid = $tag[0]['target_id'];

    // Loads the taxonomy term.
    $term = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->load($tid);

    // Gets the label.
    $name = $term->label();

    // TODO: probably a better idea would be to use uuid of those terms, since
    // the names tend to change overtime.
    // TODO: Didn't test a case with translations.
    if ($name == 'tag1') {
      // Using the old but gold function views_embed_view() to get renderable array.
      // The render function spits the html, based on views_embed_view output.
      $content = render(views_embed_view('test_view_1', 'block_1'));
    }
    elseif ($name == 'tag2') {
      $content = render(views_embed_view('test_view_1', 'block_1'));
    }
    else {
      // Case where there is not tag.
      $content = '.....';
    }
    $build = [];
    $build['default_block']['#markup'] = render($content);
    // TODO: Remove it after development. It is used only to invalidate the cache.
    $build['#cache']['max-age'] = 0;

    return $build;

  }

}
  • Thank you for your effort. I will give it a try and post my experiences as soon as possible. – tinytree Mar 17 '17 at 8:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.