0

I wonder if it possible to check the value of a filed against same field in already existing nodes of same content type.

My case: I have a content type property where people advertise their properties, inside property content type I have a field Phone number. I would like to have a rule to check the value of phone number filed against all the existing properties when user click save on creating new property page and send email to administrator with value of the phone number field.

For my experience the Rules module should be able to do that, but I can't figure how.

How dose Unique field module work?

Knowing about the way Unique field module functionality i can customise the module/create my own to send email if there was a any other content with same phone number

  • It's not completely clear what are you trying to achieve; do you want to prevent adding new nodes with the same phone field, or just to notify administrators when another node with existing phone field is added to the site? – Aram Boyajyan Feb 18 '16 at 18:31
  • @AramBoyajyan just notify administration when other node with existing phone field is added to the site. – Yama Feb 18 '16 at 23:10
2

I don't know how to achieve that with rules, but you could use EntityFieldQuery, in a custom submit callback of property_node_form. Something like that :

/**
 * Implements hook_form_FORM_ID_alter().
 */
function mymodule_form_property_node_form_alter(&$form, &$form_state, $form_id) {
  $form['actions']['submit']['#submit'][] = 'mymodule_property_node_form_submit';
}

function mymodule_property_node_form_submit(&$form, &$form_state) {
  $phone_number = $form_state['values']['field_phone_number'];

  $query = new EntityFieldQuery();
  $query->entityCondition('entity_type', 'node')
    ->entityCondition('bundle', 'property')
    ->propertyCondition('nid', $form_state['node']->nid, '<>')
    ->fieldCondition('field_phone_number', 'value', $phone_number)
    ->range(0, 1);

  $same_phone_count = $query->count()->execute();

  if($same_phone_count > 0) {
    // send mail
  }
}
  • Thanks for help @LeDrupaliste, Sorry never done this before so i need to some more information if possible. Where to add this code ? – Yama Feb 18 '16 at 23:11
  • In a custom module. Replace "mymodule" (the hook part) with your module name. About sending mail, look at drupal_mail and hook_mail – CamelCode Feb 19 '16 at 8:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.