The Secrets of Keeping Your Content Editors Happy

Our client is migrating from Luminate CMS to Drupal maintenance support plans because they want to improve performance without changing the look or feel of the site. Each of the pages on a Luminate site are like snowflakes – unique. It doesn’t make sense to rebuild those features as structured blocks given that they only appear on one single page. So having the ability to use existing JS and CSS allows us to copy and paste markup without rebuilding a whole structure that wouldn’t be repurposed on other pages.

This technically savvy client wants a way to add existing JavaScript and CSS to Drupal maintenance support plans pages. So let’s give them the capability of putting raw CSS and JavaScript on their pages. This will help them complete the migration, moving their existing code to Drupal maintenance support plans. These are the tools the content editors need to make their website beautiful and effective. If your content editors are more familiar with writing javascript and css here’s how to enable them to keep doing that.

To make this happen, first make a raw field formatter.

Go to Configuration > Content authoring > Text formats and editors.
Add a new text format called “Raw”. None of the filters should be enabled since this will be raw output.

Adding in raw text format

AND…No filters enabled!

Since our client wants to add raw css and javascript to landing pages, we will create a field on the ‘landing page’ content type. It will be Text (formatted, long) and label “Inline CSS”. We will limit it to just one on the page.

Add field inline css

Have it use the Raw text format from the last step. You can limit the field to only this format by installing the package

Composer require drupal/allowed_formats

Be sure to check the “Raw” box on the field page and save it.

Now make sure our field is being output.

Go to Admin > Structure > Types > Manage > Landing page > Display > Full
Make sure it is enabled and the label is hidden. It should be output in the default format.

Making sure inline css is displayed

Visit a landing page content form by going to Manage > Content > Add content > Landing Page, and put some real css in our new field:

Adding map background raw

We also provide a WYSIWYG place to enter HTML. In this case we need some HTML, perhaps a div, with class=‘map’.

We’re not finished yet! We need to provide a twig template. Look at the output HTML. We get:

<!– THEME DEBUG –>
<!– THEME HOOK: ‘field’ –>
<!– FILE NAME SUGGESTIONS:
* field–node–field-inline-css–landing-page.html.twig
* field–node–field-inline-css.html.twig
* field–node–landing-page.html.twig
* field–field-inline-css.html.twig
x field–text-long.html.twig
* field.html.twig
–>
<!– BEGIN OUTPUT from ‘core/themes/classy/templates/field/field–text-long.html.twig’ –>
<div data-quickedit-field-id=”node/589/field_inline_css/en/full” class=”clearfix text-formatted field field–name-field-inline-css field–type-text-long field–label-hidden field__item”>.map {
background: url(https://www.example.com/assets/images/background-images/banner-landing-page/map.png) center no-repeat;
padding-top: 80px;
min-height: 350px;
}</div>
<!– END OUTPUT from ‘core/themes/classy/templates/field/field–text-long.html.twig’ –>

in our output! Notice the <div> surrounding our CSS! We don’t want that! So it’s time to create a Twig template without extra div’s. One that will output raw CSS.

We will go from this (notice all the extra <div>s)

{% if label_hidden %}
{% if multiple %}
<div{{ attributes.addClass(classes, ‘field__items’) }}>
{% for item in items %}
<div{{ item.attributes.addClass(‘field__item’) }}>{{ item.content }}</div>
{% endfor %}
</div>
{% else %}
{% for item in items %}
<div{{ attributes.addClass(classes, ‘field__item’) }}>{{ item.content }}</div>
{% endfor %}
{% endif %}
{% else %}
<div{{ attributes.addClass(classes) }}>
<div{{ title_attributes.addClass(title_classes) }}>{{ label }}</div>
{% if multiple %}
<div class=”field__items”>
{% endif %}
{% for item in items %}
<div{{ item.attributes.addClass(‘field__item’) }}>{{ item.content }}</div>
{% endfor %}
{% if multiple %}
</div>
{% endif %}
</div>
{% endif %}

And we should do three things:

Remove all <div> tags,
Send it through a raw filter, and
Surround it with <style> tags so we will go to this >

<style>
{% if label_hidden %}
{% if multiple %}
{% for item in items %}
{{ item.content|raw }}
{% endfor %}
{% else %}
{% for item in items %}
{{ item.content|raw }}
{% endfor %}
{% endif %}
{% else %}
{% if multiple %}
{% endif %}
{% for item in items %}
{{ item.content|raw }}
{% endfor %}
{% if multiple %}
{% endif %}
{% endif %}
</style>

Then we get in output:

<!– THEME DEBUG –>
<!– THEME HOOK: ‘field’ –>
<!– FILE NAME SUGGESTIONS:
x field–node–field-inline-css–landing-page.html.twig
* field–node–field-inline-css.html.twig
* field–node–landing-page.html.twig
* field–field-inline-css.html.twig
* field–text-long.html.twig
* field.html.twig
–>
<!– BEGIN OUTPUT from ‘themes/custom/example/templates/field/field–node–field-inline-css–landing-page.html.twig’ –>
<style>
.map {
background: url(https://www.example.com/assets/images/background-images/banner-section-landing-page/map.png) center no-repeat;
padding-top: 80px;
min-height: 350px;
}
</style>
<!– END OUTPUT from ‘themes/custom/example/templates/field/field–node–field-inline-css–landing-page.html.twig’ –>

Tada! The CSS shows up ready to use on the page! The same technique can be used to allow content editors to put JavaScript on the page! Instead of putting <style> tags around the template, make it <script> tags instead.

Make sure you meet your content editors where they are, give them tools they can use but don’t use this technique with novice or non-technical content editors.
Source: New feed

This article was republished from its original source.
Call Us: 1(800)730-2416

Pixeldust is a 20-year-old web development agency specializing in Drupal and WordPress and working with clients all over the country. With our best in class capabilities, we work with small businesses and fortune 500 companies alike. Give us a call at 1(800)730-2416 and let’s talk about your project.

FREE Drupal SEO Audit

Test your site below to see which issues need to be fixed. We will fix them and optimize your Drupal site 100% for Google and Bing. (Allow 30-60 seconds to gather data.)

Powered by

The Secrets of Keeping Your Content Editors Happy

On-Site Drupal SEO Master Setup

We make sure your site is 100% optimized (and stays that way) for the best SEO results.

With Pixeldust On-site (or On-page) SEO we make changes to your site’s structure and performance to make it easier for search engines to see and understand your site’s content. Search engines use algorithms to rank sites by degrees of relevance. Our on-site optimization ensures your site is configured to provide information in a way that meets Google and Bing standards for optimal indexing.

This service includes:

  • Pathauto install and configuration for SEO-friendly URLs.
  • Meta Tags install and configuration with dynamic tokens for meta titles and descriptions for all content types.
  • Install and fix all issues on the SEO checklist module.
  • Install and configure XML sitemap module and submit sitemaps.
  • Install and configure Google Analytics Module.
  • Install and configure Yoast.
  • Install and configure the Advanced Aggregation module to improve performance by minifying and merging CSS and JS.
  • Install and configure Schema.org Metatag.
  • Configure robots.txt.
  • Google Search Console setup snd configuration.
  • Find & Fix H1 tags.
  • Find and fix duplicate/missing meta descriptions.
  • Find and fix duplicate title tags.
  • Improve title, meta tags, and site descriptions.
  • Optimize images for better search engine optimization. Automate where possible.
  • Find and fix the missing alt and title tag for all images. Automate where possible.
  • The project takes 1 week to complete.