[themes] how to theme [class/id] a nord_form? SOLVED

sebastian inforazor at gmail.com
Wed Nov 11 21:47:37 UTC 2009


Hello comrades,

So I wasn't sure how to reference specific fields when most of the 
"standard" fields only have an id or class with something ultra generic 
like: "collapse-processed" or "filefield" ; which all the other fields 
also say...

So instead I wrote a function which finally achieved what I wanted: I'd 
be curious if this is not the best way to approach this though? as in 
future projects I want to repeat this again as it really does provide 
for a MUCH cleaner interface to just turn completely OFF anything that I 
don't think a user needs 99% of the time. The only thing missing here 
right now, is a user (role) check to toggle whether they are shown or 
not [I still want to see them, for odd occasions, but the 'client' 
doesn't need to]
I use "node_type to actually figure out which node is in question as 
otherwise i can't do very good specific theming! I finally figured out 
today how to traverse the $form Array; which is a big help...
;)

Code follows:

function phptemplate_node_form($form) {
   $output = "\n<div class=\"node-form\">\n";

   // Admin form fields and submit buttons must be rendered first, because
   // they need to go to the bottom of the form, and so should not be 
part of
   // the catch-all call to drupal_render().
   $admin = '';

   $buttons = drupal_render($form['buttons']);

   // Everything else gets rendered here, and is displayed before the 
admin form
   // field and the submit buttons.
   $output .= "  <div class=\"standard\">\n";

	$output .= "<div class='" . $form['type']['#value'] . "'>\n";
  	$output .= drupal_render($form['title']);
     	$output .= drupal_render($form['field_page_area']);
	
	if ( $form['type']['#value'] == "wide_image") {
   		$output .= drupal_render($form['field_wide_image']);
	} else {
		$output .= drupal_render($form['body_field']);
	}
	
	$output .= "<div class='hidden'>\n";
	$output .= drupal_render($form);
	
   $output .= "  </div>\n";

   if (!empty($admin)) {
     $output .= "  <div class=\"admin\">\n";
     $output .= $admin;
     $output .= "  </div>\n";
   }
   $output .= $buttons;
   $output .= "</div>\n";
   $output .= "</div>\n";
   $output .= "</div>\n";

   return $output;
}


Christopher M. Jones wrote:
> What you could do is create overrides for each form element type, adding 
> an id or class based on the name or some other attribute of the form 
> element. I've done this to style specific form elements in forms. That 
> might be simpler than unsetting properties in the form array, and you 
> could do it all from within your template rather than having to create a 
> whole module. Have a look at the theme functions in the API 
> documentation to see how you can do this.
> 
> sebastian wrote:
>> Hi Thanks Lee, others,
>>
>> My ideal solution would be if the form elements had id or class names, 
>> that were unique, so that I could simply hide [display:none] the form 
>> elements that I don't feel like the less-tech savvy content admins need 
>> to see.
>>
>> My back-up solution was turning them off as a function, as part of the 
>> drupal_render function.
>>
>> This is for the node creation, of several node types; but right now I am 
>> just trying the basic: node->create->page type.
>>
>> Couple of questions:
>>
>> 1. is there a way to inject class and/or node ID's to the basic 
>> node_edit form? I would want the title to always have something like 
>> "id="node_title" on ALL of my node edit forms; whether it is for adding 
>> a 'page' type, or a new type not yet imagined but latter added. [that 
>> way if I am always hiding the 'publishing options' section, for a 
>> particular user, it would be hidden site-wide on all forms]
>>
>> 2. if it is NOT possible to do the above, then my backup is to use 
>> drupal_render(), as described below. Am I correct that I would just use:
>>
>> unset($form['name_of_element']);
>> drupal_render($form);
>>
>> ?
>>
>> 3. If there is anyway I could take my work [above] and make it into a 
>> module for others to benefit from this change, I would love to 
>> contribute this to the drupal community. As I think I must not be the 
>> only person who wants to style specific elements on the node-creation 
>> page, and right now there is simply no way to target these fields in CSS...
>>
>> I've never contributed before though, and still learning, so not sure if 
>> whatever changes I make will be compatible as a "module"
>>
>> Thank you so much for your help!!
>>
>> Sebastian.
>>
>> Lee Rowlands wrote:
>>>> When I have a nord_form, either for creation or editing, I get a lot of 
>>>> id's like: #thmr_81, thmr_86, #thmr_91, #thmr_98
>>> These are inserted by the devel_themer module and should not be relied upon
>>>
>>>> But if I replace $output .= drupal_render($form); with
>>>>
>>>> $output .= drupal_render($form['title']);
>>>> $output .= drupal_render($form['body_field']);
>>>> $output .= drupal_render($form['custom_select_field']);
>>>>
>>>> Which are the only three I actually need to have displayed for a 
>>>> non-admin, then the page will NOT save, [no errors, it just reloads the 
>>>> page and erases all my changes]
>>> The call to drupal_render($form) actually renders the hidden elements such
>>> as form id and build id which are needed to determine if the form was
>>> submitted by the Form API. If these aren't found, the Form API thinks it
>>> wasn't submitted and hence no submit function is called and no data is
>>> saved.
>>> You need to examine the $form variable and unset (using the unset function)
>>> any array members (form elements) you don't want then add a final
>>> drupal_render($form) to the end of your output and thiss will add all the
>>> hidden stuff (and anything else that still yet to be rendered).
>>> I'm assuming you don't have a module here? Otherwise you could do the
>>> unsetting in hook_form_alter.
>>>
>>> _______________________________________________
>>> themes mailing list
>>> themes at drupal.org
>>> http://lists.drupal.org/mailman/listinfo/themes
>>>
>> _______________________________________________
>> themes mailing list
>> themes at drupal.org
>> http://lists.drupal.org/mailman/listinfo/themes
>>
> _______________________________________________
> themes mailing list
> themes at drupal.org
> http://lists.drupal.org/mailman/listinfo/themes
> 


More information about the themes mailing list