There are many times where in drupal I'm styling a theme and if I don't need some element I cannot remove in settings I just hide it with css(display:none, or similar). Is it considered a bad practice to do this and should I always use hooks or some over methods?
-
Can you give a few examples of unwanted elements?– mpdonadio ♦Commented Feb 3, 2013 at 23:48
-
Yes, hiding via css is bad practice, since it will anyway generated on Drupal, loaded to browser and make some js/css calculations. So best way is theming/coding on Drupal side. For your task hiding depend what you want to hide: form, page elements...– NikitCommented Feb 4, 2013 at 1:47
-
examples would be unwanted labels, links, some kind of extra options for a field(show row weights, edit etc.) sometimes whole fields in comments section(only one i'm using a hook for it).– staskoCommented Feb 4, 2013 at 15:03
2 Answers
It depends what you are hiding.
If it is something like a page or block title or labels or other semantic type markup, you should hide it using the element-invisible class, which effectively hides it but without using display: none. This way screen readers can still see the text, which helps give visually impaired users context for the subsequent content, which is required for accessibility reasons.
If you are hiding actual content, like fields, or whole blocks, or menus etc. you should not be doing it in the CSS. It is best to do it on the back end.
The time when this is most important is hiding for access control. Never use css to hide data or fields that a users should not be allowed to see. Because that data is still available to the user, it just isn't visible by default. So that would be a security issue.
For forms you can use hook_form_alter(), hook_form_BASE_FORM_ID_alter() or hook_form_FORM_ID_alter().
For other areas, there are many possibilities depending on what you are themeing and what you are intending to do. Some examples, are hook_node_view_alter() for node pages, hook_user_view_alter() for user pages, other alter hooks, template preprocessors, theme functions and template file overrides.
For more information on drupal hooks see http://api.drupal.org For more information on overriding theme functions and template files see http://drupal.org/theme-guide/6-7
The best way is to use hook_form_alter to get the form and unset the form elements.
Another approach is to make the element type to hidden. i prefer the first one.