Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Hi,
I don't know if it's a Wysiwyg module's issue but i can't align image inserted in Ckeditor from the media browser.
When i select the image, then click the "align center" wysiwyg's button, the image is well aligned.
Disabling the Rich Text editor show that the image is embeded in a div with align="center".
But when i click save, the image remain left aligned. When i look at the source code, the image is embeded outside the
.
Anybody have a solution?
Comments
Comment #1
lelizondo CreditAttribution: lelizondo commentedI think this is a duplicate #1043570: Floating images left and right
Comment #2
camdarley CreditAttribution: camdarley commentedAs i doubt, I bring back my issue there. Actually, when I edit my content, my image is like this:
<p style="text-align: center; ">[[{"type":"media","view_mode":"media_large","fid":"283","attributes":{"alt":"","class":"media-image","title":"","typeof":"foaf:Image"}}]]</p>
And when I save it, the source look like this:
Why is the image embeded outside the
<p>
?Comment #3
oxyc CreditAttribution: oxyc commentedI don't know if a module respects this but what I know is that according to the W3C specifications
<p>
elements are only allowed to contain phrasing content which the<div>
element is not. The output would be invalid HTML.Actually I can confirm this, I have stripped my field.tpl.php of all the markup and the above code works.
Comment #4
lelizondo CreditAttribution: lelizondo commentedYes, it is true,
<div>
are not supossed to be inside a<p>
element, but this is one of the things that makes Drupal uncompetitive against other CMS's.Let's think of the user who never heard about the W3C and wants to center a picture but it can't because Drupal is acting 'smart' when in fact, from a user experience point of view, is not. How easy is to center a picture in Joomla / Wordpress? Very.
I hope there's a workaround for this kind of problem. I'm not even the user who posted this issue in the first place, but I certainly understand the frustration when a simple thing like this one cannot be done.
Comment #5
camdarley CreditAttribution: camdarley commentedActually, I'm using Drupal to create websites and allow clients to post their own news, article, contents...
So when clients ask me "How can i center the image i added in my text?"... I don't know what to answer although i agree oxyc explanation.
I know media module and a lot of other modules made for Drupal 7 are still perfectible. But as lelizondo said, the lake of simple features like this one is prejudicial to Drupal adoption. But maybe that's the price of being so polyvalent.
Comment #6
oxyc CreditAttribution: oxyc commentedI do agree with you but this is really up to the WYSIWYG javascript library. TinyMCE uses
display: block; margin-left: auto; margin-right: auto;
on the<img>
element itself when you center align. This is a very good way to do it and no divs, spans or whatever will be able to break it.If however the WYSIWYG library uses
style="text-align: center"
to center it then it's up to the library to see the issue and switch from a paragraph to something else.Some possible solutions
- If the WYSIWYG library has an option to select HTML-blocks it should work if you change from paragraph to a div and then center align the div. (assuming the library isn't strict about using the
<p>
element).- Make a copy of the field.tpl.php template stripped of all markup and move it to your theme directory renamed as field--file.tpl.php
- Same as above but replace the
<div>
elements with<span>
.Comment #7
lelizondo CreditAttribution: lelizondo commentedThe
<span>
might be the easiest, it could actually work.The question here is if this could be solved by the Media module without hacking core. I started reading some code and media module has a function to remove the label when a field is added using WYSIWYG. Maybe is possible to remove all the divs by doing some black magic alter with the Field API. I don't really know, I'm not familiar with the Field API.
Comment #8
camdarley CreditAttribution: camdarley commentedI modified field--file.tpl.php as oxyc said and it work (replacing
<div>
with<span>
). But i agree there could be a better solution. Maybe the solution be found in WYSIWYG module, isn't it?Comment #9
oxyc CreditAttribution: oxyc commentedI've looked into this a bit and found the following comment in
wysiwyg-media.js
:This is true but to my knowledge jQuery.fn.html() takes care of all the quirks (and the quirks shouldn't even apply to this case, the problem is with tables and whitespace). It would be only natural the the wysiwyg libraries would implement something similar since a solution already exists.
Anyways @lelizondo, apparently the intention is for the wysiwyg to also use the field.tpl.php template. I'm thinking this would also fix the problem as the wysiwyg would know it had to center align a block element while it know believes it is only an image.
Comment #10
oxyc CreditAttribution: oxyc commentedNevermind this seems to be related to contenteditable and a solutions seems very difficult since there are a ton of available editors.
Comment #11
lelizondo CreditAttribution: lelizondo commentedSo, what do we do about this. Postpone it? Of maybe won't fix?
Comment #12
oxyc CreditAttribution: oxyc commentedI've been trying to figure out what the issue is here, but I can't reproduce what the comment is describing nor find a reference to something that would prevent this from working.
Could someone with further knowledge or the one who wrote the comment explain further what the issue is?
Comment #13
lelizondo CreditAttribution: lelizondo commentedEnable WYSIWYG & Media and add the button to the editor to add inline media.
Configure your Input Formats to have this order:
Create a node, using the WYSIWYG and Media add an image. Center the image using the "Text Center" button in the editor.
Save the node.
Take a look at the HTML of the page and you'll see what @camdarley is talking about in #2
Comment #14
oxyc CreditAttribution: oxyc commentedSorry I was a bit unclear,
I meant the issue with inserting wrapping divs described in the inline comment mentioned #9. I'll see if I can get some unit tests on this later next week.
Comment #15
JacobSingh CreditAttribution: JacobSingh commentedNice research folks!
So, this isn't really a media bug per se, but we should fix it. I guess we could either get ckeditor to do alignment in a more standards compliant way OR stop Drupal from fixing the HTML. We can't remove all of the divs from media (although I think right now, we are a little too verbose). There will certainly be cases where we embedding video, links, etc and I don't think we can be sure we'll *never* have a div. Plus, the divs we have in place allow for consistent formatting when media is used as a field, or elsewhere.
Comment #16
lelizondo CreditAttribution: lelizondo commentedGreat news, patch in http://drupal.org/node/1043570#comment-4258282 fixes both issues. I'm marking this as duplicate.
Comment #17
sokrplare CreditAttribution: sokrplare commentedReally hate to reopen a long-closed issue like this one, but the description is spot on for the 2.x dev branch it would appear. Floats are working great (as fixed in #1043570: Floating images left and right), but unless I'm missing something (sure hope I am!) the centering functionality has been lost. Any ideas?
Comment #18
sokrplare CreditAttribution: sokrplare commentedComment #19
mengi CreditAttribution: mengi commentedHaving the same issue. No option to center align images.
Comment #20
nosikus CreditAttribution: nosikus commentedI am also having the same issue. Image is not aligning to the centre.
Comment #21
sterlingedsrv@gmail.com CreditAttribution: sterlingedsrv@gmail.com commentedSame problem here too.
Comment #22
lykyd CreditAttribution: lykyd commentedSame here
Comment #23
maggiehu CreditAttribution: maggiehu commentedSame here. I searched and read many posts, but still am lost. I tried to use both
. Does anyone know what is the reason?
Thanks.
Comment #24
bkosborneAccording to this post it's not possible to center images using the standard image plugin that ships with CKEditor.
Instead, I was able to use the Enhanced Image Plugin (also known as Image2) which provides much better capabilities for handling images.
After adding the necessary plugins to my CKEditor plugins folder (lineutils, widget, image2), I was able to get this sort of working. After inserting an image using the Media dialog, I can then save the page, then edit again. Once editing, the Image2 plugin kicks in and allows me to align the image, but I can no longer edit the image using the Media dialog anymore (even tho the token remains in tact).
Comment #25
bkosborneI initially closed this thinking it was probably a WYSIWYG module issue (maybe it is), but it's possibly an integration issue in this module as well (the media_wsysiwyg module). Will try and dig into this.
Comment #26
bkosborneComment #27
osopolarYou may center images by default (via css) and use the ckeditors left/right alignment function to override that setting. See issue #1515186: Current best practice for WYSIWYG Image Insert #14.
Comment #28
skyredwang#24 isn't a work around, as the root case in #2 is still here.
Comment #29
skyredwangHowever, #1 is fixed in the latest dev.