Help needed: How to empty specific fields in a form


Is there an easy way to empty fields in a form (say, for instance, field Title in Items/Edit page)?

I’ve tried with a custom filter,

// Define Filters
protected $_filters = array(
	'emptyItemTitle' => array('ElementForm', 'Item', 'Dublin Core', 'Title')

and a custom function,

public function emptyItemTitle($components, $args)
	$components['inputs'] = get_view()->formTextarea('', '', array(
		'cols' => 50,
		'rows' => 3,
	return $components;

but it messes up, as it’s replacing the whole element, with its classes, with a single input element that doesn’t have the correct parameters.

So, but for using JS to empty the value of the Title’s input field, I was wondering whether there’s another (easier) way.


Beyond just trying to blank out the value on the form, what are you trying to accomplish?

It’s possible to do what you want with a filter (we pass along the necessary data to create a form element with the correct name so the form will work with the new element) but I’m not immediately seeing why you’d want to blank an input on edit like that.

Hello, @jflatnes.

It’s all part of my attempt to make a plugin for Items Duplication: I load all metadata in a page that is (almost) exactly like the Items/Edit one, but that saves the data as it was a Items/Add one. Now, I thought it could be useful to blank out some fields (Title, Subject and Date, f.i.), so that user doesn’t risk to create a copycat of the original Item (I’ve chosen those fields because they’re the most likely to change).

How could I accomplish what you were suggesting, John?

I think you’ll find the documentation a little clearer on what you need to do in the more targeted ElementInput filter. Basically, we pass along the “stem” of the input name (the part that uniquely identifies the value but without the last component, like [text] or [is_html]). There’s a small example of what you’d do to replace a textarea with a single-line text field, which should be pretty trivially adaptable to what you’re talking about doing.

Thanks, John.
It works like a charme, now. For those who may be interested, here’s the code (I’ve added also a kind of highlighting, just to remind user that the field needs to be filled in):

$components['input'] = get_view()->formTextarea($args['input_name_stem'] . '[text]', '', array(
	'cols' => 50,
	'rows' => 3,
	'autofocus' => 1,
	'style' => 'background-color: ' . ITEM_DUPLICATOR_HIGHLIGHT_COLOR

I was wondering whether there’s a simple way to remove the extra input fields, now. Say the original Item has got two instances of Subject field; when emptying them, I think it would make more sense to get rid of the second one, leaving it up to the user to decide whether to add any extra one. Any idea?