-
Rendering a Drupal 8 Link with Anchor and Destination
I’m working on a site that contains several complicated views (many exposed filters). The views render entities using a couple different view modes. These view modes use Display Suite. Within these view modes I’ve leveraged DS custom fields for several needs.I needed to render a node “Edit” link within some of these view modes. The user would click the edit link, edit the node, then click “Save”. The system would then bring the user to the exact position of the page they were on before, filters and settings intact.This type of solution would work without Display Suite (leveraging other methods). You’re getting a DS-based example because that’s what the site called for.In my particular case I needed to separate the anchor from the edit link. If you’re okay having them in the same place you could skip Step 1 and just include the anchor within the markup for the edit link. -
Joining Strings in a Drupal Views Field using Twig
I have two optional fields on a Drupal 8 content type: City and State. Both are rendered as simple text.
In a fields-based View I wanted to show the field output as “Portland” or “Portland, OR”, or “OR”.
First, I added the two fields, State then City (the order is important). Next I excluded the State field from display.
Finally, I opened the Rewrite Results pane of the City field and checked the Override the output of this field with custom text checkbox.
After several attempts at using Twig’s join, replace, spaceless, and more, I landed on this simple solution as the rewrite text:
1234567{% if field_member_city %}{{- field_member_city -}}{% if field_member_state %}, {% endif %}{% endif %}{% if field_member_state %}{{- field_member_state -}}{% endif %}Note the whitespace modifiers (hyphens). These are the key to getting Portland, OR instead of Portland , OR .
Also, we cannot just use {{field_member_city}}, {{field_member_state}} because we could end up with , OR if City is empty.
-
Drupal 8 Search API Tips
Here are some quick tips for configuring Search API for Drupal 8.
Easy Indexing
If you’re like me you rely heavily on Display Modes (aka View Modes) for your entity types. The most convenient way to get Search API to index your data is to use the Search Index view mode on any entity types you will index. If you’re using Display Suite you may want to choose a layout that doesn’t include authoring information, published date, etc. Just fill this view mode, in each entity type, with the fields you want to index. If you plan to use this to drive the output, you should also hide labels and do whatever else will ensure clean output; more on this later.
I suggest indexing the Title (in the case of content types) separately. More on that in the Boost notes below.
-
Drupal Views – Show One (Most Recent) Item Per Group
Alright, the title is a bit misleading because I’m not using Groups, but it is the best explanation of what I’m achieving with this example. My goal is to show a list of the most recent blog post in each category, where clicking the category takes you to the associated post.
I have a content type called Blog Post which has the following fields:
- Date (field_blog_date – date field – single value)
- Category (field_blog_tr_category – taxonomy reference field – single value)
- Items (field_blog_cr_items – content reference field – unlimited values)
-
Friendly URL for Views Contextual Filter on a Boolean Field
I have a story content type that has a corresponding Views listing page. The stories can be flagged as Featured through a checkbox/boolean field ( field_story_featured ). The stories also have a Story Type value ( field_story_type ). The views listing page needs to have the following links at the top:
The URL structure for the Views page makes the most sense as follows (purple = story type contextual filter, orange = featured contextual filter):
-
Quickly Implement a Block as a Paragraph in Drupal 7
Disclaimer: As with most tasks in Drupal, there are several ways to do this. My goal here was to see how quickly it could be done without writing code and without introducing unnecessary complexity.
Goal — quickly implement a newsletter listing (using Views) that can be dropped into any paragraph field and can have a unique title per use.
The output would be a simple block from Views. We already use Display Suite, so naturally we use a DS-centric approach.
-
Drupal Quick Tip: Easier “Archive” Filtering
I was working on a project today where we needed to show an archive of announcement nodes. The sidebar had to show a listing of months like this:
This is easy to accomplish by using the default Archive view that ships with Views, or by creating your own view that uses the same contextual filters, block and page. The contextual filter most typically used is Content: Created year + month. Here’s what the settings look like for this contextual filter:
-
Views Exposed Filter: Terms for current language only
I’ve been working on a multilingual site that has a product finder. We have 3 exposed filters that allow a user to select an Activity, IP Category, or Industry. All of these filters are Taxonomy Term filters. Some of the terms throughout those vocabularies have a specific language set, and should only show when that language is active.
The first step is to make sure the output (Views results) only shows products where the node language matches the current language. This is easy using the built in Node translation: Language = Current user’s language filter.
-
Proximity by City or Zip Code in Drupal 6 with Location and Views
The location module for Drupal 6 is a robust module. On most projects, it gives us 100 percent of what we need. Proximity searching based on zip code is built-in and fairly painless to setup. I’ve recently been tasked to also allow proximity searching based on City. As you might imagine, the request is very similar. Both methods require the use of latitude and longitude (decimals). The difference is that instead of querying the database for latitude and longitude based on zip code (it’s usually termed postal code in Views, Location, etc.), we’re asking for the coordinates of a city. You’ll find that many cities have multiple zip codes, each of which is a row in the location module’s zipcodes database table. In this example, I’m not giving a real honest attempt at solving this issue, but rather I just return the first coordinate that matches the city.
-
Get a List of Top-level Taxonomy Terms
Quick Tip: Getting a list of taxonomy terms is easy using a view (type: Taxonomy). Trying to get only terms at the highest level is a bit trickier. One method I came up with is to add a Taxonomy: Parent term argument on the view. The key is to tell the argument to Provide default argument of PHP Code and set the value to return 0;
The result is that only top-level terms (terms without parents) are returned.