In hook_form_alter() is there a way to hide all form elements, then specify which ones to display? Right now, I'm manually hiding them all, then showing them as needed.
Thanks!

function portfolio_form_alter(&$form, $form_state, $form_id) {
	if (arg(0) == 'edit_portfolio_pages') { 
		hide($form['field_portfolio_settings']);
		hide($form['field_portfolio_colors']);
		hide($form['field_portfolio']);
		hide($form['title']);
		hide($form['additional_settings']);
		$form['actions']['preview']['#access'] = FALSE;
		$form['actions']['delete']['#access'] = FALSE;
	}

	if (drupal_get_path_alias() == "edit_portfolio_pages/" . arg(1) . "/content") {
	    show($form['field_portfolio']);
	}

}

Comments

tomas.teicher’s picture

As I know, each key in form array, which doesn't start with "#" is considered to be form element. You can create loop through $form array and set all items without '#'.

jaypan’s picture

Calling the function element_children() and passing it the $form returns all the keys that don't begin with a # sign.

Contact me to contract me for D7 -> D10/11 migrations.

Dan Silver’s picture

That's very good to know. Thanks!