0

I have two node bundles: events and registrations. Registrations have an entity reference field pointing to a given event.

I have a view, that list registrations with some event and registrant information and a contextual filter for given event. I would like to add this view as a tab to Event node edit form so, that it would list current registrations to given event.

I managed to display the view on the edit form easily by adding the view to form render array with event nid as an argument for the contextual filter, but not sure, what are the possible ways to add view to a node edit form as a tab.

1
  • Welcome to Drupal Answers! 👋🏿 "Best" always is difficult to be answered. As best always is an opinion. Unfortunately I don't understand what "add (a) view to a node edit form as a tab" actually means. Does it involve JavaScript? Can you maybe update your question and try to add more information and/or context? Do you mean "how can I add a view as a tab to a node edit form"? Where is this tab supposed to be located? Tab in the sense of hidden by default and visible if clicked? Similar to an accordion item?
    – leymannx
    Commented Oct 12, 2020 at 21:39

1 Answer 1

1

I'm assuming you mean "Drupal tab" when you say you want to display Views as a tab on the Event edit form. "Drupal tabs" are the "View | Edit | Translate | Etc" links that are usually visually styled as tabs.

Although, a Drupal tab is an independent page, it will not be like a Block within the Edit form, it will be just a link on the "View | Edit" menu, you'll have "View | Edit | Registrations". This means you won't see registrations listed when you go to Edit (unless you add a Block to that page through regular procedure), you will see your Registration results when you visit the "Registrations" link/page.

Display a Views Page as one of Drupal tabs

You will need to have the "Page" Display in your Registrations Views. Page Displays have special Page settings that include the Path and Menu options. Additionally, you'll need to adjust the Contextual filter too, to only appear on Event type nodes.

You can combine these two settings to make that Views Page appear among the Drupal tabs of a set of links.

The Path needs to follow the logic of the set of links. In your case you have drupal tabs /node/[nid], /node/[nid]/edit etc. You'll want to set your Path to node/%node/registrations.

On the Menu you should choose the Menu tab option, fill in what you want the tab text to say (into "Menu link title"), choose a Parent, it can some menu you're not displaying anywhere if you don't need this page to appear as a menu item elsewhere. The Weight will affect the order on the Drupal tabs, enter a larger number (e.g. 50) to make sure it comes up at the end, and not before "View" or "Edit".

You already have the Contextual filter configured and working, so you might not need to do anything else. Check that the Contextual filter ID from the "Content" Category with these settings:
When the filter value is NOT available
Provide default value
Content ID from URL
When the filter value IS in the URL or a default is provided
Specify validation criteria
Validator: Content
Content type Event
Set the option for the case when the argument doesn't validate to something that makes sense to you.

When you save this View and reload some Event node page you should be able to see the Registrations as a Drupal tab. Rebuild Drupal cache if it doesn't appear.

4
  • Thanks. Fixing the view path resolved the issue. I had /admin prefix there. I added now a RouteSubscriber to set _admin_route to use admin theme for the view. For some reason Registrations tab link is now visible on all content types, but that's a minor issue.
    – joumak
    Commented Oct 13, 2020 at 7:53
  • I mentioned you control that in the Contextual filter settings. You need to have the Validation tell the Views which Content Types to show up on, and what to do if the argument doesn't belong to that Content Type. Have you checked that?
    – prkos
    Commented Oct 13, 2020 at 16:37
  • Atm. I have following settings in Contextual filter config for Content: Event: Relationship -> Do not use relationship; When the filter value is not in the url: Provide default value -> Content ID from URL; Exceptions: (not set); Skip default argument for view URL: false; When the filter value is in the url or default is provided: Specify validation criteria: Validator (Content) -> Content type: Event; Multiple arguments: Single ID; Action to take if filter value does not validate: Show "Page not found"; (In Relationships there is: field_event: Content (require this relationship))
    – joumak
    Commented Oct 14, 2020 at 8:25
  • Is it working? Without testing it I think your Contextual is ok, you're using the Event field, not the ID field, but that is actually correct. And the configuration seems ok. I only don't understand what the Relationship is for, I don't think you need it just for Drupal tabs. Keep the Relationship if you need it for other things (showing Event info for each Registration or similar), but try to Unrequire it, or remove it completely, although it doesn't seem it should affect it.
    – prkos
    Commented Oct 14, 2020 at 23:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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