0

I am attempting to create a view and paragraph combination that allows for site authors to customize the content that they want to display.

What is currently working:

  • Within my site is the content type of Article
  • I have a view, called News, that is set up to display the published Articles.
  • I created a Paragraph called Article Scroller (article_scroller) that uses a Views reference field (field_view_ref_news) to display the above view.

The above setup allows authors to add the News view to any page, such as a Basic Page or another custom content type. This is all working great.

Where I'm stuck:

I want to take this a step further and allow for the content author to define a filter for the view, such as a location. To do this, I added an Entity Reference field to my Article content type called "Location" (field_location). This is populated with a value from a taxonomy list of states (e.g. Texas).

I then added this same field (field_location) to my article_scroller paragraph.

The idea is that an author would add Texas to an article, and then if they wanted to customize the view to show Texas articles only, they would also define this term within the paragraph.

From there, I'm not sure how to proceed. I'm pretty sure I need to add some kind of relationship and contextual filter here, but am struggling because the term/tid is not going to appear within the URL, and the options under "Provide default value" don't seem to be relating to what I want to do (at least, nothing I've tried from other posts and research has seemed to apply).

Another confusing thing is that I only have my one "field_location" field, but within the view, it appears as two different fields - one under the "Content" category and the other under the "Paragraph" category, which makes sense, but seems to complicate the contextual filters even more as I have to choose between "Content: Location" and "Paragraph: Location".

I know that one alternative method would be to create a view page/block for each location, Texas, California, New York, etc. I am just hoping I can do something a bit more dynamic.

1 Answer 1

0

Have you seen these Modules, i have not used them but they look like they do the job.

Otherwise you could preprocess your paragraph and render the view

function renderView($viewId, $displayId, $arguments)
{
    $result = false;
    $view = Views::getView($viewId);

    if (is_object($view)) {
        $view->setDisplay($displayId);
        $view->setArguments($arguments);
        $view->execute();

        $result = \Drupal::service('renderer')->render($view->render());
    }

    return $result;
}

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.