CKEditor https://publish.mediacurrent.com/ en Adding Custom Styles and Classes to Your WYSIWYG https://publish.mediacurrent.com/blog/adding-custom-styles-and-classes-your-wysiwyg <span class="field field--name-title field--type-string field--label-hidden">Adding Custom Styles and Classes to Your WYSIWYG</span> <div class="clearfix text-formatted field field--name-body field--type-text-with-summary field--label-hidden field__item"><p>More and more these days, we need to add custom styles to content that the user enters. This can be a challenge for front end developers as we don't know how the information will be entered and have less control over the structure. One thing we can do to help content editors is to provide them with classes and styles based on these classes that they can add in the WYSIWYG.</p> <p>As this is not a how to set up a WYSIWYG tutorial, I will just state that we use CKEditor.</p> <p>Once CKEditor is installed, you can begin configuring it and adding your own styles and functionality.</p> <p>The config page allows you to choose the CKEditor mode that you want to customize. In this example, I am editing the 'full' profile. </p> <p><strong>Config page: </strong></p> <style type="text/css"> <!--/*--><![CDATA[/* ><!--*/ <!--td {border: 1px solid #ccc;}br {mso-data-placement:same-cell;}--> /*--><!]]>*/ </style><p><a href="https://ckeditor.com/docs/ckeditor4/latest/api/CKEDITOR_config.html" target="_blank">https://ckeditor.com/docs/ckeditor4/latest/api/CKEDITOR_config.html</a></p> <p><left></left><img alt="config screen" data-entity-type="" data-entity-uuid="" src="https://publish.mediacurrent.com/sites/default/files/screenshot_2016-02-17_10.07.22.png" />The tab we are interested in is 'CSS'.</p> <p>The first configuration that we want to make is to 'Editor CSS'. As the help text suggests, this allows you to add specific css for the editor. This is what the person who inputs content will see on the admin page. Because we rename our css file, we need to use the Define CSS option. </p> <p>We need to define the path. The help text suggests several shortcut methods. This is how to use the theme path, which is what you will most often use.</p> <p><img alt="" data-entity-type="" data-entity-uuid="" src="https://publish.mediacurrent.com/sites/default/files/screenshot_2016-02-17_10.27.49.png" /></p> <p>The field input is: %tcss/ckeditorstyles.css. In this case our css is inside ThemeFolder/css. <em>Note that there is no forward slash between the %t and the css subfolder.</em></p> <p>In this example I have a separate stylesheet that relates to items in the WYSIWYG. However you could link to your main.styles.css sheet, or any style sheet that you define these styles in. I chose to make a smaller style sheet so I wasn't loading a large sheet that was mostly unused.</p> <p>The next configuration we are going to make is to the Predefined Styles. As with the css we want to define the path to the file. The helper text says:</p> <p><em>'CKEditor is highly configurable. The most commonly used features are listed below. You can also adjust CKEditor to your needs by changing the ./sites/all/modules/contrib/ckeditor/ckeditor.config.js configuration file.'</em></p> <p>This is what we want to do, so we are going to copy this file, and move it to our theme js folder. Once we have done that we can enter its location in the Predefined styles path. As with the css path, we want the format to be %tjs/ckeditor.styles.js or whatever your structure. The important part again being no leading / before the subfolder.</p> <p>Now we have set up our custom paths, and have either moved or created the custom files, we can add our new styles. If you open the 'ckeditor.config.js' file we copied, you can see that the styles are fairly well documented, if not weirdly spaced. I won't explain what is documented in the file, but it should be clear how to add new classes and elements to your set up.</p> <p><strong>What should you do when your changes don't appear? </strong><br /> So this happens a lot and is the main reason I wanted to write this as you can lose hours trying to figure out why the additions you have made do not appear.</p> <p>The first thing to do is to make sure that the settings in the config are mapping to the correct files. By this, I mean that you are pointing the custom settings to the corresponding files in your theme.</p> <p>You can check in the DOM, or use the Network tab in the inspector, to see if the custom css file is being called. The next check is to make sure that the WYSIWYG is in the same mode as the one that you added the custom settings to. In my example, I used 'full'. The setting for this is on the content piece you are editing here: </p> <p><img alt="" data-entity-type="" data-entity-uuid="" src="https://publish.mediacurrent.com/sites/default/files/screenshot_2016-02-18_11.46.34.png" /></p> <p>If you are seeing some of your custom styles that are defined in the js file in the editor but not all, there are several things that you can check. First, is the editor set up to allow the tag that you are defining? Second, if you added the style as a button to the editor, does the editor have that button? These can be checked here, on the CKEditor config page, under Editor Appearance: <br /> <img alt="" data-entity-type="" data-entity-uuid="" src="https://publish.mediacurrent.com/sites/default/files/screenshot_2016-02-18_12.04.09.png" /></p> <p><strong>What if edits to your js file are not showing up? </strong></p> <p>This is the most common issue that I have had. First, I would recommend trying to get as many edits done in between testing as possible. Obviously that isn't that easy to do, and you will always have to make changes, but it is useful to think in terms of making as few updates as possible. The reason for this is that the module seems to either cache or use a timestamp for the custom js file that is hard to break. One work around I had locally was that for every time I made changes to create a copy of the file, and move it to a subfolder, and then reference that in the config settings. As this is the first time that the file has been called you can see the changes.</p> <p>My workflow for this was to make the changes to the file at themename/js/ckeditor.styles.js. Then create a tmp folder in js and copy the file to it, example themename/js/tmp/a/ckeditor.styles.js . Then go to admin/config/content/ckeditor/edit/(mode) and add the new path %tjs/tmp/a/ckeditor.styles.js. Clear caches and enjoy the new styles. </p> <p>I hope this helps you, I know the information is out there, but I had to look in several places so hopefully, this will shorten your search. </p> <p>My Sources<br /> <a href="https://www.drupal.org/project/ckeditor" style="line-height: 1.538em;">CKEditor - WYSIWYG HTML editor</a><br /> <a href="https://www.drupal.org/node/2442821" style="line-height: 1.538em;">Drupal 7: Installation of CKEditor vs. CKEditor and WYSIWYG</a></p> </div> <span class="field field--name-uid field--type-entity-reference field--label-hidden"><a title="View user profile." href="/about/our-team/mediacurrent-team" lang="" about="/about/our-team/mediacurrent-team" typeof="schema:Person" property="schema:name" datatype="" class="username">Mediacurrent Team</a></span> <span class="field field--name-created field--type-created field--label-hidden">Thu, 02/25/2016 - 14:13</span> <div class="field field--name-field-tags field--type-entity-reference field--label-above"> <div class="field__label">Tags</div> <div class="field__items"> <div class="field__item"><a href="/tags/drupal" hreflang="en">Drupal</a></div> <div class="field__item"><a href="/tags/ckeditor" hreflang="en">CKEditor</a></div> <div class="field__item"><a href="/tags/wysiwyg" hreflang="en">WYSIWYG</a></div> <div class="field__item"><a href="/tags/css" hreflang="en">CSS</a></div> <div class="field__item"><a href="/tags/how" hreflang="en">how to</a></div> </div> </div> <div class="field field--name-field-related-content field--type-entity-reference field--label-above"> <div class="field__label">Related Content</div> <div class="field__items"> <div class="field__item"><a href="/blog/pro-tip-how-write-conditional-css-ie10-and-11" hreflang="en">Pro Tip: How to Write Conditional CSS for IE10 and 11</a></div> <div class="field__item"><a href="/blog/supplying-thumbnails-your-headless-drupal-front-end" hreflang="en">Supplying Thumbnails to your Headless Drupal Front End</a></div> <div class="field__item"><a href="/blog/responsive-images-d8" hreflang="en">Responsive Images in D8 </a></div> </div> </div> <div class="gatsby-iframe-container"><iframe class="gatsby-iframe" src="https://preview-misriptide.gtsb.io/blog/adding-custom-styles-and-classes-your-wysiwyg"></iframe></div> Thu, 25 Feb 2016 19:13:14 +0000 Mediacurrent Team 2431 at https://publish.mediacurrent.com A Comparison of D7 Image Caption Methods using WYSIWYG https://publish.mediacurrent.com/blog/comparison-d7-image-caption-methods-using-wysiwyg <span class="field field--name-title field--type-string field--label-hidden">A Comparison of D7 Image Caption Methods using WYSIWYG</span> <div class="clearfix text-formatted field field--name-body field--type-text-with-summary field--label-hidden field__item"><p>I recently had the opportunity to explore popular methods of adding captions to images inside the WYSIWYG editor using the setup of WYSIWYG module with CKEditor library. Our two main criteria were Media module integration and a styled caption in the WYSIWYG editor. As I discovered, we couldn’t have both without custom coding which the budget didn’t allow for. Media module integration won out and the File Entity with View Modes method was chosen. </p> <p>The modules/methods I reviewed were:</p> <ul> <li>CKEditor’s Image2 plugin</li> <li>Caption Filter module</li> <li>JCaption module</li> <li>Image Caption module</li> <li>File entity and view modes method</li> </ul> <p><span style="line-height: 1.538em;">These are the <a href="#summary">results of the module and method comparisons</a>. A <a href="#comparison">matrix with various properties of each</a> is at the end of the article.</span></p> <h2>CKEditor Image2 Plugin:</h2> <p>Enhanced Image (Image2) is a CKEditor plugin meant to replace the ordinary image plugin. It gives captioning capabilities in addition to center alignment and click and drag resize. The easiest way to install it is to use <a href="https://www.drupal.org/sandbox/quiron/2219417">Quiron’s sandbox project</a> and follow the instructions given. The only deviation I needed to make was to rename the module’s directory from ckeditor_image_captions to ckeditor_image2. </p> <p><strong>Sample output of a captioned image:</strong></p> <pre> &lt;img alt="This is alt text" height="150" src="http://placehold.it/350x150" width="350"&gt;This is my awesome image2 caption</pre><p><img alt="CKEditor's Image2 plugin modal" class="media-element file-original-media" height="318" src="https://publish.mediacurrent.com/sites/default/files/image2-1.png" typeof="foaf:Image" width="290" /></p> <p>Image2’s replacement of default Image modal</p> <p><img alt="Image2 Plugin nicely formats the caption with the image inside the WYSIWYG" class="media-element file-original-media" height="359" src="https://publish.mediacurrent.com/sites/default/files/image2-2.png" title="What image caption looks like in WYSIWYG with Image2 plugin" typeof="foaf:Image" width="507" /></p> <p>What image caption looks like in WYSIWYG with Image2 plugin</p> <p><img alt="Image2 plugin is not formatted in node view. This must be added." class="media-element file-original-media" height="291" src="https://publish.mediacurrent.com/sites/default/files/image2-3.png" title="What image caption with Image2 looks like in node view by default. It could use some styling." typeof="foaf:Image" width="637" /><br /> <span style="line-height: 1.538em;">What image caption with Image2 looks like in node view by default. It could use some styling.</span></p> <p> </p> <h2>Caption_filter:</h2> <p><a href="https://www.drupal.org/project/caption_filter">Caption_filter</a> is a token replacement method of adding a caption. Captioning is achieved by adding the caption text inside of [caption][/caption] tokens in source view. For example:</p> <pre> [caption align=center]&lt;img alt="This is alt text" height="150" src="http://placehold.it/350x150" width="350" /&gt;[/caption]</pre><p><strong>Sample output of a captioned image:</strong></p> <pre> &lt;div class="caption caption-center"&gt;  &lt;div class="caption-inner" style="width: 350px;"&gt;   &lt;img alt="This is alt text" height="150" src="http://placehold.it/350x150" width="350"&gt;This is my awesome caption filter caption  &lt;/div&gt; &lt;/div&gt;</pre><p><img alt="Image shows switching to source view to use caption filter" class="media-element file-original-media" height="291" src="https://publish.mediacurrent.com/sites/default/files/caption-filter1.png" title="Using caption filter requires working in Source view or disabling the WYSIWYG editor." typeof="foaf:Image" width="589" /><br /> Using caption filter requires working in Source view or disabling the WYSIWYG editor.</p> <p><img alt="Image shows the image and caption in a wysiwyg. The raw tokens are displayed in the WYSIWYG." class="media-element file-original-media" height="331" src="https://publish.mediacurrent.com/sites/default/files/caption-filter2.png" title="What the caption looks like in WYSIWYG. Tokens are not replaced in the editor. Caption Filter is probably best used without the use of a WYSIWYG." typeof="foaf:Image" width="598" /><br /> What the caption looks like in WYSIWYG. Tokens are not replaced in the editor. Caption Filter is probably best used without the use of a WYSIWYG</p> <p><img alt="Image and caption are nicely styled by default on node view" class="media-element file-original-media" height="330" src="https://publish.mediacurrent.com/sites/default/files/caption-filter3.png" title="Default styling of Caption Filter’s captions." typeof="foaf:Image" width="672" /><br /> Default styling of Caption Filter’s captions.</p> <p> </p> <h2>JCaption:</h2> <p>This image caption module uses jQuery to transform the title or alt attribute of an image to an image caption. See the <a href="https://www.drupal.org/project/jcaption">JCaption module page</a> for another comparison of the JCaption, Image Caption and Caption Filter modules.</p> <p><strong><span style="line-height: 1.538em;">Sample output of a captioned image:</span></strong></p> <pre> &lt;span class="caption none" style="width: 350px;"&gt;   &lt;img alt="This is my awesome alt/caption text" src="http://placehold.it/350x150"&gt;   &lt;p&gt;This is my awesome alt/caption text&lt;/p&gt; &lt;/span&gt;</pre><p><img alt="Image shows the CKEditor default plugin modal - how a JCaption is added" class="media-element file-original-media" height="504" src="https://publish.mediacurrent.com/sites/default/files/jcaption1.png" title="Adding an image with alt text via the CKEditor default image plugin" typeof="foaf:Image" width="460" /><br /> Adding an image with alt text via the CkEditor default image plugin.</p> <p><img alt="Applying a caption with JCaption - the caption is not visible in the WYSIWYG." class="media-element file-original-media" height="323" src="https://publish.mediacurrent.com/sites/default/files/jcaption2.png" title="A view of the image in WYSIWYG editor. The caption is not visible in the editor." typeof="foaf:Image" width="531" /><br /> A view of the image in WYSIWYG editor. The caption is not visible in the editor.</p> <p><img alt="Default styling of the image in node view is a simple display of the image followed by the caption underneath" class="media-element file-original-media" height="312" src="https://publish.mediacurrent.com/sites/default/files/jcaption3.png" title="Default styling of the image in node view" typeof="foaf:Image" width="390" /><br /> Default styling of the image in node view</p> <p> </p> <h2>Image Caption</h2> <p>With the <a href="https://www.drupal.org/project/image_caption">Image Caption module</a>, an image is added with the usual CKEditor Image plugin, and the caption is added by setting a previously defined class in the Font Style’s dropdown of the WYSIWYG editor. JQuery is used to wrap the image in an html container div and then puts the image title text in a child div underneath the image.</p> <p><strong>Sample output of a captioned image:</strong></p> <pre> &lt;span class="image-caption-container image-caption-container-none" style="display: inline-block; width: 350px;"&gt;   &lt;img alt="" class="caption caption-processed" src="http://placehold.it/350x150" title="This is the awesome Image Caption" style="width: 350px; height: 150px;" /&gt;   &lt;span style="display:block;" class="image-caption"&gt;This is the awesome Image Caption&lt;/span&gt; &lt;/span&gt;</pre><p><img alt="Image displays without caption in the WYSIWYG when using Image Caption module" class="media-element file-original-media" height="323" src="https://publish.mediacurrent.com/sites/default/files/image-caption1.png" style="line-height: 1.538em;" title="Image inserted into the WYSIWYG. Image displays without caption." typeof="foaf:Image" width="512" /></p> <p><span style="line-height: 1.538em;">Image inserted into the WYSIWYG. Image displays without caption.</span></p> <p><img alt="Image caption and image as shown in node view with a simple format - unstyled image with caption underneath" class="media-element file-original-media" height="320" src="https://publish.mediacurrent.com/sites/default/files/image-caption2.png" title="Image as seen with Image Caption caption in node view." typeof="foaf:Image" width="459" /><br /> Image as seen with Image Caption caption in node view.</p> <h2> </h2> <h2>File Entity and View modes:</h2> <p>This method involves the most amount of setup but integrates well with the Media module. It involves adding a caption field to the image file entity, an image style, and a view mode, and configuring the image file display. <a href="http://www.58bits.com/blog/2013/03/06/how-to-add-captions-to-images-in-drupal">A step-by-step account of this method can be found at 58 Bits</a>.</p> <p><strong><span style="line-height: 1.538em;">Sample output of a captioned image:</span></strong></p> <pre> &lt;div class="media media-element-container media-caption"&gt;   &lt;div id="file-8" class="file file-image file-image-gif contextual-links-region"&gt;   &lt;h2 class="element-invisible"&gt;&lt;a href="/file/8"&gt;500x400.gif&lt;/a&gt;&lt;/h2&gt;   &lt;div class="contextual-links-wrapper contextual-links-processed"&gt;   &lt;a class="contextual-links-trigger" href="#"&gt;Configure&lt;/a&gt;   &lt;ul class="contextual-links"&gt; &lt;li class="file-edit first"&gt;&lt;a href="/file/8/edit?destination=node/4"&gt;Edit&lt;/a&gt;&lt;/li&gt;   &lt;li class="file-delete last"&gt;&lt;a href="/file/8/delete?destination=node/4"&gt;Delete&lt;/a&gt;&lt;/li&gt;   &lt;/ul&gt;   &lt;/div&gt;   &lt;div class="content"&gt;   &lt;img height="176" width="220" class="media-element file-caption" typeof="foaf:Image" src="http://captions.dev/sites/default/files/styles/medium/public/500x400_5.gif?itok=IsKmYwfN" alt=""/&gt;   &lt;div class="field field-name-field-caption field-type-text field-label-hidden"&gt;   &lt;div class="field-items"&gt;   &lt;div class="field-item even"&gt;This is the awesome File Entity caption&lt;/div&gt;   &lt;/div&gt;   &lt;/div&gt;   &lt;/div&gt;   &lt;/div&gt;</pre><p><img alt="The image is added to the WYSIWYG via the media browser by choosing the caption view mode created during setup" class="media-element file-original-media" height="666" src="https://publish.mediacurrent.com/sites/default/files/file-entity1.png" title="Adding the image and caption via the media browser. Choose the view mode in the “Display as” field and set the Caption field." typeof="foaf:Image" width="592" /><br /> Adding the image and caption via the media browser. Choose the view mode in the “Display as” field and set the Caption field.</p> <p><img alt="The WYSIWYG with a captioned image - the caption doesn't display" class="media-element file-original-media" height="347" src="https://publish.mediacurrent.com/sites/default/files/file-entity2.png" title="Image in WYWIWYG - no caption visible." typeof="foaf:Image" width="516" /><br /> Image in WYWIWYG - no caption visible.</p> <p><img alt="Image in node view contains no styling - shows just an image with a caption displayed underneath" class="media-element file-original-media" height="341" src="https://publish.mediacurrent.com/sites/default/files/file-entity3.png" title="Image as seen in node view with no styling." typeof="foaf:Image" width="354" /><br /> Image as seen in node view with no styling.</p> <p> </p> <h2 id="comparison">A Comparison of Captioning Modules/Methods.</h2> <table border="1" cellpadding="1" cellspacing="1" style="width: 500px;"> <caption>Comparison matrix of captioning modules/methods.</caption> <thead> <tr> <th scope="col"> </th> <th scope="col">Image2</th> <th scope="col">Caption Filter</th> <th scope="col">JCaption</th> <th scope="col">FE &amp; View Mode</th> <th scope="col">Image Caption</th> </tr> </thead> <tbody> <tr> <th>Integrates with Media</th> <td> </td> <td style="text-align: center">x</td> <td> </td> <td style="text-align: center">x</td> <td> </td> </tr> <tr> <th>Visible in WYSIWYG</th> <td style="text-align: center">x</td> <td style="text-align: center">x (although without token replacement)</td> <td> </td> <td> </td> <td> </td> </tr> <tr> <th>Each instance of the image can have a different caption</th> <td style="text-align: center">x</td> <td style="text-align: center">x</td> <td style="text-align: center">x</td> <td> </td> <td style="text-align: center">x</td> </tr> <tr> <th>Works with Ckeditor Image</th> <td>Replaces Image plugin</td> <td style="text-align: center">x</td> <td style="text-align: center">x</td> <td> </td> <td style="text-align: center">x</td> </tr> <tr> <th>Easy to set up</th> <td style="text-align: center">x</td> <td style="text-align: center">x</td> <td style="text-align: center">x</td> <td> </td> <td style="text-align: center">x</td> </tr> <tr> <th>Source view required</th> <td> </td> <td style="text-align: center">x</td> <td> </td> <td> </td> <td> </td> </tr> <tr> <th>Additional notes</th> <td>Also gain image centering capabilities</td> <td>Nicely styled node view out of the box without additional CSS</td> <td>Many config options including whether to use alt text or title text for caption</td> <td>Most extensive set up out of all options</td> <td>Requires adding a class to the WYWIWYG’s Font Style dropdown</td> </tr> </tbody> </table> <h2> </h2> <h2 id="summary">Summary: Which Module Do I Use?!?</h2> <p>Which module is used is really dependent on the requirements of the project. If you require Media module functionality (maintaining a library of images that can be searched and re-used), the only two options are the File Entity - View Mode method and the Caption Filter module. If your editors are savvy and understand html basics, Caption Filter will give you more flexibility around captions, allowing you to reuse images with different captions tailored to the context. If you need to keep it as simple as possible, doing the extra work of setting up the File Entity and View Modes will make it pretty simple for your editors to add pictures, have them automatically resized, and aligned without any additional work.</p> <p>If the Media module is not needed, Image2, JCaption, and Image Caption are all pretty comparable, although I would lean towards the Image2 plugin simply because it gives the ability to center the image and the caption is visible in the WYSIWYG editor. The one thing I would caution against is in the JCaption module. It gives the option of using either alternative text* or title text as the caption. It’s generally not good practice to simply duplicate the alt text (you are adding alt text to your images, right?) for a caption. Both the caption and the alt text will be read by screen readers and will be redundant for the user. Using title text as caption text may be less problematic since most screen readers do not read title text by default.</p> <p>*Alternative text (or alt text) is text that describes an image. It gives people who are blind and use screen readers and people who have images turned off due to low bandwidth a description of the image that they can’t see. It is good practice to include alt text for images.</p> </div> <span class="field field--name-uid field--type-entity-reference field--label-hidden"><a title="View user profile." href="/about/our-team/mediacurrent-team" lang="" about="/about/our-team/mediacurrent-team" typeof="schema:Person" property="schema:name" datatype="" class="username">Mediacurrent Team</a></span> <span class="field field--name-created field--type-created field--label-hidden">Tue, 03/24/2015 - 13:18</span> <div class="field field--name-field-tags field--type-entity-reference field--label-above"> <div class="field__label">Tags</div> <div class="field__items"> <div class="field__item"><a href="/tags/drupal" hreflang="en">Drupal</a></div> <div class="field__item"><a href="/tags/development" hreflang="en">Development</a></div> <div class="field__item"><a href="/tags/module" hreflang="en">Module</a></div> <div class="field__item"><a href="/tags/wysiwyg" hreflang="en">WYSIWYG</a></div> <div class="field__item"><a href="/tags/ckeditor" hreflang="en">CKEditor</a></div> </div> </div> <div class="field field--name-field-related-content field--type-entity-reference field--label-above"> <div class="field__label">Related Content</div> <div class="field__items"> <div class="field__item"><a href="/blog/guide-drupal-terminology" hreflang="en">A Guide to Drupal Terminology</a></div> </div> </div> <div class="gatsby-iframe-container"><iframe class="gatsby-iframe" src="https://preview-misriptide.gtsb.io/blog/comparison-d7-image-caption-methods-using-wysiwyg"></iframe></div> Tue, 24 Mar 2015 17:18:09 +0000 Mediacurrent Team 2032 at https://publish.mediacurrent.com