Hello,

I'm working on a node type called Project and another one called Contact.
The Project have some Entity Reference fields, with Contact selection.

To search/select, create and edit Contact entities inside the Project edit form, i'm using References Dialog module. I'm not sure that it's the perfect one for that, but it can perfectly search contact Entity References from a View.
That's the context !

Now, i would like to display search, create, edit and remove Contacts links directly from Project node view or from a table of Projects with Views module. Users will no longer need to go through the Project edit form to edit Contacts.

Any ideas ?

Thank you in advance for your reply.
Nicolas.

Comments

nikodev’s picture

Hello,

Maybe i find a solution.

To add (and search via References dialog) or delete some entity references directly from parent node view, i use form_alter hook.
For example, to have a add link, i use the node edit link with parameters (field name, action, destination).
URL: /node/xx/edit?field=field_project_contact&action=add&destination=projets

To have a delete link, i use the node edit link too with same parameters (but not the same action parameter value)
URL: /node/xx/edit?field=field_project_contact&action=remove&destination=projets

In my form_alter hook, i have the following code :

...
// Get the parameters in $out variable
$parsed_url = parse_url($form['#action']);
$url_query = $parsed_url['query'];
parse_str($url_query, $out);
			
if (isset($out["field"]) && isset($out["action"]))
{
	// Hide all fields except the one which i send through the "field" parameter
	foreach($form as $key => $value)
	{
		if (substr($key, 0, 7) == "field_p")
		{
			if ($key != $out["field"])
			{
				$form[$key]["#access"] = FALSE;
			}
		}
	}	
	
	// Some labels
	$texts = array(
		"remove" => array(
			"title" => "Remove an element from your project",
			"message" => "<p>Are you sure... ?</p>",
			"button" => "Remove",
		),
		"add" => array( 
			"title" => "Add an element in your project",
			"message" => "",
			"button" => "Add",
		),
	);

	// IF action parameter is REMOVE, i remove all datas about this field and i hide the field in edit form
	if ($out["action"] == "remove")
	{
		$form[$out["field"]]["#access"] = FALSE;
		$form["#entity"]->$out["field"] = array();
		$form["#node"]->$out["field"] = array();
		$form[$out["field"]]["und"] = null;
	}

	// Change form labels
	$form['some_text']['#markup'] = $texts[$out["action"]]["message"];
	$form['actions']['submit']['#value'] = $texts[$out["action"]]["button"]; 
	drupal_set_title($texts[$out["action"]]["title"]);

	// Hide the delete button to be sure that the user doesn't something stupid...
	$form['actions']['delete']['#access'] = FALSE; 
}

Voilà.
Nicolas.