{"id":355,"date":"2012-10-09T19:22:57","date_gmt":"2012-10-10T03:22:57","guid":{"rendered":"https:\/\/maxbronsema.com\/blog\/?p=355"},"modified":"2012-10-09T19:22:57","modified_gmt":"2012-10-10T03:22:57","slug":"image-captions-in-drupal-7-a-method","status":"publish","type":"post","link":"https:\/\/maxbronsema.com\/blog\/2012\/10\/09\/image-captions-in-drupal-7-a-method\/","title":{"rendered":"Image Captions in Drupal 7 &#8211; A method"},"content":{"rendered":"<p>Image captions are supposed are a fairly standard item in a web developer&#8217;s toolkit. In a modern CMS they seem to be a given. However, Drupal leaves this to the contrib space, as it should, since each site has different needs. However, the lack of an adopted contrib method for achieving image captions was a bit surprising.<\/p>\n<p>With Drupal 7 the media module is used to handle media. No problem, however it doesn&#8217;t do anything to allow for automatic captions to appear. However, it does allow us to show users when they upload an image that an alt tag is a good idea and a title tag can be used. Most auto-caption techniques rely on that title tag to be used as the caption and you show it with a bit of javascript and CSS.<\/p>\n<p>My goal for the end user was to have them get captions for free but have the ability to turn them off. I do not want them to assign a special class for a caption to appear because in my use case, the majority of folks want the captions. However, to disable a caption I want a fairly straight forward method that can be accomplished in the WYSIWYG editor.<\/p>\n<p>To make this work I leveraged several contrib modules and now have it working except it still will not automatically show in the WYSIWYG editor. That last issue seems to be somewhere that improvements can still be made.<\/p>\n<h3>Ingredients (List of Modules):<\/h3>\n<ul>\n<li><a href=\"http:\/\/drupal.org\/project\/media\">Media Module<\/a> 7-x.1.2<\/li>\n<li><a href=\"http:\/\/drupal.org\/project\/insert\">Insert<\/a><\/li>\n<li><a href=\"http:\/\/drupal.org\/project\/image_caption\">Image Caption<\/a><\/li>\n<li><a href=\"http:\/\/drupal.org\/project\/wysiwyg\">WYSIWYG<\/a> with the CKeditor Library<\/li>\n<li>HTML Purifier (Not needed if you allow full HTML)<\/li>\n<li>Any dependencies of the above modules, i.e. ctools, views, etc&#8230;<\/li>\n<\/ul>\n<h3>Configuration:<\/h3>\n<p>Go ahead and download the modules and install them on your site whatever your preferred method. You can enable them as well.<\/p>\n<p>Now, most of these modules auto-configure themselves. For this example we will use the basic page content type but add an image field.<\/p>\n<figure id=\"attachment_359\" aria-describedby=\"caption-attachment-359\" style=\"width: 550px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/maxbronsema.com\/blog\/wp-content\/uploads\/2012\/10\/Screen-Shot-2012-10-09-at-7.49.03-PM.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-large wp-image-359\" title=\"Screen Shot 2012-10-09 at 7.49.03 PM\" src=\"https:\/\/maxbronsema.com\/blog\/wp-content\/uploads\/2012\/10\/Screen-Shot-2012-10-09-at-7.49.03-PM-1024x277.png\" alt=\"Basic Page content type\" width=\"550\" height=\"148\" srcset=\"https:\/\/maxbronsema.com\/blog\/wp-content\/uploads\/2012\/10\/Screen-Shot-2012-10-09-at-7.49.03-PM-1024x277.png 1024w, https:\/\/maxbronsema.com\/blog\/wp-content\/uploads\/2012\/10\/Screen-Shot-2012-10-09-at-7.49.03-PM-300x81.png 300w, https:\/\/maxbronsema.com\/blog\/wp-content\/uploads\/2012\/10\/Screen-Shot-2012-10-09-at-7.49.03-PM.png 1562w\" sizes=\"auto, (max-width: 550px) 100vw, 550px\" \/><\/a><figcaption id=\"caption-attachment-359\" class=\"wp-caption-text\">Basic page content type configuration screen.<\/figcaption><\/figure>\n<p>Notice that we use the image widget which is augmented via the media module. The next screen we need to edit is the images field. Make sure to check the Enable <em>Alt<\/em> field and Enable <em>Title<\/em> field.<\/p>\n<figure id=\"attachment_360\" aria-describedby=\"caption-attachment-360\" style=\"width: 355px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/maxbronsema.com\/blog\/wp-content\/uploads\/2012\/10\/Screen-Shot-2012-10-09-at-7.55.12-PM.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-360\" title=\"Screen Shot 2012-10-09 at 7.55.12 PM\" src=\"https:\/\/maxbronsema.com\/blog\/wp-content\/uploads\/2012\/10\/Screen-Shot-2012-10-09-at-7.55.12-PM.png\" alt=\"The alt and title field checkboxes are checked.\" width=\"355\" height=\"50\" srcset=\"https:\/\/maxbronsema.com\/blog\/wp-content\/uploads\/2012\/10\/Screen-Shot-2012-10-09-at-7.55.12-PM.png 355w, https:\/\/maxbronsema.com\/blog\/wp-content\/uploads\/2012\/10\/Screen-Shot-2012-10-09-at-7.55.12-PM-300x42.png 300w\" sizes=\"auto, (max-width: 355px) 100vw, 355px\" \/><\/a><figcaption id=\"caption-attachment-360\" class=\"wp-caption-text\">Don&#8217;t forget to enable the alt and title fields.<\/figcaption><\/figure>\n<p>Right below those check boxes, if you have the Insert module enabled you will see a collapsed field labeled insert. Expand it. Check the Enable Insert button check box. Choose the styles you want to make available to your users. Add the word &#8220;caption&#8221; without the quotes to the Additional CSS Classes text field. This will add the caption class to every image inserted into your nodes. This makes the caption magic happen automatically, as long as you fill out the title field with each image, because the image caption module looks for the class caption on all images.<\/p>\n<figure id=\"attachment_361\" aria-describedby=\"caption-attachment-361\" style=\"width: 550px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/maxbronsema.com\/blog\/wp-content\/uploads\/2012\/10\/Screen-Shot-2012-10-09-at-7.56.54-PM.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-large wp-image-361\" title=\"Screen Shot 2012-10-09 at 7.56.54 PM\" src=\"https:\/\/maxbronsema.com\/blog\/wp-content\/uploads\/2012\/10\/Screen-Shot-2012-10-09-at-7.56.54-PM-1024x419.png\" alt=\"The insert configuration screen.\" width=\"550\" height=\"225\" srcset=\"https:\/\/maxbronsema.com\/blog\/wp-content\/uploads\/2012\/10\/Screen-Shot-2012-10-09-at-7.56.54-PM-1024x419.png 1024w, https:\/\/maxbronsema.com\/blog\/wp-content\/uploads\/2012\/10\/Screen-Shot-2012-10-09-at-7.56.54-PM-300x122.png 300w, https:\/\/maxbronsema.com\/blog\/wp-content\/uploads\/2012\/10\/Screen-Shot-2012-10-09-at-7.56.54-PM.png 1518w\" sizes=\"auto, (max-width: 550px) 100vw, 550px\" \/><\/a><figcaption id=\"caption-attachment-361\" class=\"wp-caption-text\">Enable the insert button, choose some styles, and add caption to the classes field.<\/figcaption><\/figure>\n<p>Make sure you have the image caption module enabled and let&#8217;s make a new test node.<\/p>\n<p>Go to Content -&gt; Add Content -&gt; Basic Page and title your new node to test the image caption.<\/p>\n<p>Then go down to your image field and upload a new image or graphic. Fill in the alt field and the in the title field add the text you want for your caption. Click the insert button near the style dropdown menu and the image appears in your post.<\/p>\n<p><a href=\"https:\/\/maxbronsema.com\/blog\/wp-content\/uploads\/2012\/10\/Screen-Shot-2012-10-09-at-8.02.10-PM.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-medium wp-image-362\" title=\"Screen Shot 2012-10-09 at 8.02.10 PM\" src=\"https:\/\/maxbronsema.com\/blog\/wp-content\/uploads\/2012\/10\/Screen-Shot-2012-10-09-at-8.02.10-PM-300x61.png\" alt=\"An image field being configured in Drupal 7.\" width=\"300\" height=\"61\" srcset=\"https:\/\/maxbronsema.com\/blog\/wp-content\/uploads\/2012\/10\/Screen-Shot-2012-10-09-at-8.02.10-PM-300x61.png 300w, https:\/\/maxbronsema.com\/blog\/wp-content\/uploads\/2012\/10\/Screen-Shot-2012-10-09-at-8.02.10-PM.png 966w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>Now, click the preview button and you will see the caption appear beneath your image. Styling the caption class is up to you.<\/p>\n<p>&nbsp;<\/p>\n<figure id=\"attachment_364\" aria-describedby=\"caption-attachment-364\" style=\"width: 300px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/maxbronsema.com\/blog\/wp-content\/uploads\/2012\/10\/Screen-Shot-2012-10-09-at-8.03.03-PM.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-364\" title=\"Screen Shot 2012-10-09 at 8.03.03 PM\" src=\"https:\/\/maxbronsema.com\/blog\/wp-content\/uploads\/2012\/10\/Screen-Shot-2012-10-09-at-8.03.03-PM-300x244.png\" alt=\"A picture of the author with a caption below.\" width=\"300\" height=\"244\" srcset=\"https:\/\/maxbronsema.com\/blog\/wp-content\/uploads\/2012\/10\/Screen-Shot-2012-10-09-at-8.03.03-PM-300x244.png 300w, https:\/\/maxbronsema.com\/blog\/wp-content\/uploads\/2012\/10\/Screen-Shot-2012-10-09-at-8.03.03-PM.png 328w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><figcaption id=\"caption-attachment-364\" class=\"wp-caption-text\">Look ma&#8217; a caption!<\/figcaption><\/figure>\n<p>Fairly awesome.<\/p>\n<h3>Why this approach:<\/h3>\n<p>OK, so why did I decide to do it this way? There are a few other modules, notably <a href=\"http:\/\/drupal.org\/project\/jcaption\">jCaption<\/a>. You could also make each image a node and then attach a caption field to the image. That approach seemed unrealistic as then I would have to easily allow authors to import nodes, into their post in an easy manner. However, the author would have to create at a minimum two nodes\/entities just to create a page with one image. That doesn&#8217;t entice many people.<\/p>\n<p>jCaption looks interesting except that you cannot turn off captions. Anything with a title attribute gets a caption. Sometimes, captions are not appropriate but people still want the contents of the title attribute to appear on mouseover. With the image caption module because the caption is controlled by a class, the user can edit the image in their WYSIWYG editor and remove the class. In the next image you can see the caption class has been removed, meaning no caption will appear. This gives control to the page creators which is a fundamental goal of CMS.<\/p>\n<figure id=\"attachment_365\" aria-describedby=\"caption-attachment-365\" style=\"width: 283px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/maxbronsema.com\/blog\/wp-content\/uploads\/2012\/10\/Screen-Shot-2012-10-09-at-8.13.23-PM.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-365\" title=\"Screen Shot 2012-10-09 at 8.13.23 PM\" src=\"https:\/\/maxbronsema.com\/blog\/wp-content\/uploads\/2012\/10\/Screen-Shot-2012-10-09-at-8.13.23-PM-283x300.png\" alt=\"The advanced property screen of the CKeditor image window.\" width=\"283\" height=\"300\" srcset=\"https:\/\/maxbronsema.com\/blog\/wp-content\/uploads\/2012\/10\/Screen-Shot-2012-10-09-at-8.13.23-PM-283x300.png 283w, https:\/\/maxbronsema.com\/blog\/wp-content\/uploads\/2012\/10\/Screen-Shot-2012-10-09-at-8.13.23-PM.png 454w\" sizes=\"auto, (max-width: 283px) 100vw, 283px\" \/><\/a><figcaption id=\"caption-attachment-365\" class=\"wp-caption-text\">No caption class means no caption accompanying the image.<\/figcaption><\/figure>\n<h3>Improvements:<\/h3>\n<p>I am happy with this approach except for the fact that users cannot see the captions directly in the WYSIWYG. It would be fantastic if that were possible and it will require more research. Since the caption appears because of jQuery we would have to utilize a drupal behavior to fire whenever an image is inserted and then with ajax save the state of the page and add in the caption. It sounds messy.<\/p>\n<p>I wish there was an easier way for users to disable the caption as well. It is not hard to go into the properties of an image but it would be great if they could just click the image and remove the caption class via a dropdown menu or contextual editor.<\/p>\n<h3>Overall:<\/h3>\n<p>This seems to be a robust approach that helps the user by minimizing the knowledge and time needed to add captions. I need to find a good way to alert them of how to remove a caption, but I am hopeful for the use of this technique.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Image captions are supposed are a fairly standard item in a web developer&#8217;s toolkit. In a modern CMS they seem to be a given. However, Drupal leaves this to the contrib space, as it should, since each site has different needs. However, the lack of an adopted contrib method for achieving image captions was a &hellip; <a href=\"https:\/\/maxbronsema.com\/blog\/2012\/10\/09\/image-captions-in-drupal-7-a-method\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Image Captions in Drupal 7 &#8211; A method<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[36,28,2],"tags":[],"class_list":["post-355","post","type-post","status-publish","format-standard","hentry","category-drupal","category-dynamic-media","category-web-development"],"_links":{"self":[{"href":"https:\/\/maxbronsema.com\/blog\/wp-json\/wp\/v2\/posts\/355","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/maxbronsema.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/maxbronsema.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/maxbronsema.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/maxbronsema.com\/blog\/wp-json\/wp\/v2\/comments?post=355"}],"version-history":[{"count":8,"href":"https:\/\/maxbronsema.com\/blog\/wp-json\/wp\/v2\/posts\/355\/revisions"}],"predecessor-version":[{"id":369,"href":"https:\/\/maxbronsema.com\/blog\/wp-json\/wp\/v2\/posts\/355\/revisions\/369"}],"wp:attachment":[{"href":"https:\/\/maxbronsema.com\/blog\/wp-json\/wp\/v2\/media?parent=355"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/maxbronsema.com\/blog\/wp-json\/wp\/v2\/categories?post=355"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/maxbronsema.com\/blog\/wp-json\/wp\/v2\/tags?post=355"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}