I have already search for solutions but these solutions did'nt help me finally...
Let me talk about my problem... I have created a module on drupal 7 and i have also created new content types which are used with entity reference. For example i have:

ResponsableA-> SessionA1-> ApprenantA1-1

ResponsableB-> SessionB1->ApprenantB1-1

And if i want to remove as content the node "SessionA1" it does not work.

In my custom module i made a function to remove nodes i want.
This is the code i wrote:

function supprimer_submit_yes($recup_arg, &$form_state){

    $recup_arg = arg(4);
    
    $query = new EntityFieldQuery();
	
	$query->entityCondition('entity_type', 'node')
	->entityCondition('bundle', 'responsable_p')
	->propertyCondition('status', 1)
	->fieldCondition('field_ref_session', 'target_id', $recup_arg)
	->range(0,1);
	$result = $query->execute();
	
	if (!empty($result['node'])) {
		$nid_responsable = array_shift(array_keys($result['node']));
		//$nodes = node_load_multiple($nids);
		//$items = entity_load('node', $nid_responsable);
		$node=node_load($nid_responsable);
	}
	
	
	$ewrapper = entity_metadata_wrapper('node', $node);
	
	unset($ewrapper->field_ref_session);
	$ewrapper->save();

    }

In my code firstly, i get the argument of the actual page with arg(): it contain the nid what i want. Here this is the nid of "SessionA1".
After i use the EntityFieldQuery() to get the nid of the parent node of "SessionA1". Here the parent node is "ResponsableA". I make a fieldCondition to target on the field wich contain "SessionA1", here it's "field_ref_session"
Once i get the "ResponsableA" nid, i load the node to remove the field which contain "SessionA1".

When i run this function with a button, it seems to make something but in reality it makes anything, the content "SessionA1" is not removed from my content.

Before i wrote this code, i have also used others fonctions and ways to remove what i want like node_delete($nid), etc but i've got the same result.
I want precise also i'm using CTools module in my custom module.

If someone can help me, i'll appreciate a lot !

PS: I'm french so if u don't understand one word or sentence of my english, please comment about that.

Comments

arulraj’s picture

If you're able to rely on the entity API module you should be able to use code similar to the following:

// Load some entity.
$entity = entity_load_single($entity_type, $id);

// Remove the field value.
unset($entity->field_FIELD_NAME[LANGUAGE_NONE][$index]);

// Reset the array to zero-based sequential keys.
$entity->field_FIELD_NAME[LANGUAGE_NONE] = array_values($entity->field_FIELD_NAME[LANGUAGE_NONE]);

// Save the entity.
entity_save($entity_type, $entity);

dims971’s picture

Thank you for your answer.

I tried your method. But i have the same problem. However i dont have messages displaying error but the content is not removed.

My code with your suggestion:

function supprimer_submit_yes($recup_arg, &$form_state){
	
	$recup_arg = arg(4);

        $query = new EntityFieldQuery();
	
	$query->entityCondition('entity_type', 'node')
	->entityCondition('bundle', 'responsable_p')
	->propertyCondition('status', 1)
	//->fieldCondition('field_nom_organisme', 'value', 'NULL', '!=');
	->fieldCondition('field_ref_session', 'target_id', $recup_arg)
	->range(0,1);
	$result = $query->execute();
	
	if (!empty($result['node'])) {
		$nid_responsable = array_shift(array_keys($result['node']));

                $entity = entity_load_single($entity_type, $nid_responsable);

                unset($entity->field_ref_session[LANGUAGE_NONE][$recup_arg]);
	
		$entity->field_ref_session[LANGUAGE_NONE] = array_values($entity->field_ref_session[LANGUAGE_NONE]);
		
		entity_save($entity_type, $entity);
          }

}

I have one question... when you write this: "unset($entity->field_FIELD_NAME[LANGUAGE_NONE][$index]);" the variable $index must be replaced by the $nid of the node i want to remove or by another variable ?

arulraj’s picture

$index is node id which you want to remove.

dims971’s picture

Okay. Good. This is exactly what i've done in my code.
$recup_arg represent the id of the node i want to remove.

There is certainly something i have forgot because i don't succed to remove my node.
Maybe due to the fact that user permission is missing in my code the node does not remove ?

Maybe i must to use another method...Do you know another method to remove field entity reference ?

Thank you again.

arulraj’s picture

$entity_type should be replaced by "node". Use below code hope it will works for you.

function supprimer_submit_yes($recup_arg, &$form_state){	
	$recup_arg = arg(4);
        $query = new EntityFieldQuery();	
	$query->entityCondition('entity_type', 'node')
	->entityCondition('bundle', 'responsable_p')
	->propertyCondition('status', 1)
	//->fieldCondition('field_nom_organisme', 'value', 'NULL', '!=');
	->fieldCondition('field_ref_session', 'target_id', $recup_arg)
	->range(0,1);
	$result = $query->execute();
	
	if (!empty($result['node'])) {
		$nid_responsable = array_shift(array_keys($result['node']));
                <strong>$entity = entity_load_single('node', $nid_responsable);	
		$entity->field_ref_session = array();		
                entity_save('node', $entity);</strong>
          }

}
dims971’s picture

Hello @arulja, after many many tests with my code to find what is the problem, i've finally found the cause.
i've noted that the variable $recup_arg which is normally equal to the id of the node of type responsable_p, in reallity this variable equal a NULL value.

I don't know why i can't get the actual page argument. It seems that the fonction arg() doesn't work on my submit fonction.

Because i have two differents button submit on my form like that:

       $form['oui']=array(
			'#type'=>'submit',
			'#value'=>'Oui',
			'#ajax'=>array(
					'callback'=>'supprimer_submit_yes'
			),
	);
	
	$form['non']=array(
			'#type'=>'submit',
			'#value'=>'Non',
			'#ajax'=>array(
					'callback'=>'supprimer_submit_no'
			),
	);

And in the function "supprimer_submit_yes' show above, the arg() function does not work.

I think the problem come from the way i made the submissions code for my two buttons because i have made another form in the same module which use one simple submit button and the arg() function work properly in the submit function which is called.
It's look like that:

$form['submit'] = array(
		'#type' => 'submit',
		'#value' => 'Modifier',		
	);

And the submit function which is called:

function modifier_session_partenaire_form_submit(&$form, &$form_state){

  $arg_session = arg(4);
    
  $node = node_load($arg_session);
  $session_wrapper = entity_metadata_wrapper('node', $node);
.....

it's worked fine.

So, i go to search another way to make my two submit buttons.

If you have any suggestions, you are welcome :)