You are here

function crm_core_relationship_ui_add_relationship_form_validate in CRM Core 8

Same name and namespace in other branches
  1. 8.3 modules/crm_core_relationship_ui/crm_core_relationship_ui.pages.inc \crm_core_relationship_ui_add_relationship_form_validate()
  2. 8.2 modules/crm_core_relationship_ui/crm_core_relationship_ui.pages.inc \crm_core_relationship_ui_add_relationship_form_validate()
  3. 7 modules/crm_core_relationship_ui/crm_core_relationship_ui.pages.inc \crm_core_relationship_ui_add_relationship_form_validate()

Perform validation for add relationship form.

1 string reference to 'crm_core_relationship_ui_add_relationship_form_validate'
crm_core_relationship_form in modules/crm_core_relationship_ui/crm_core_relationship_ui.pages.inc
Form builder for CRM Activity forms.

File

modules/crm_core_relationship_ui/crm_core_relationship_ui.pages.inc, line 257
CRM Core Relationship UI Pages.

Code

function crm_core_relationship_ui_add_relationship_form_validate($form, &$form_state) {
  $relationship_type = $form_state['values']['relationship_type'];
  $reverse = $form_state['values']['reverse'];
  $source_contact = _crm_core_relationship_ui_get_contact_from_autocomplete_field_value($form_state['values']['source_contact']);
  $destination_contact = _crm_core_relationship_ui_get_contact_from_autocomplete_field_value($form_state['values']['destination_contact']);
  if (!$relationship_type || !is_object($relationship_type)) {
    form_set_error('relationship_type', t('Relationship type is required.'));
    return;
  }
  if (!crm_core_relationship_is_relationship_type($relationship_type)) {
    form_set_error('relationship_type', t('Relationship type is not valid CRM relationship type.'));
    return;
  }
  if (!$source_contact) {
    form_set_error('source_contact', t('Please, input source contact.'));
    return;
  }
  if (!$destination_contact) {
    form_set_error('destination_contact', t('Please, input destination contact.'));
    return;
  }
  $source_bundles = array_keys(crm_core_relationship_load_contact_types($relationship_type, 0));
  if (!in_array($source_contact->type, $source_bundles)) {
    form_set_error('source_contact', t('Please, input source contact of proper type.'));
    return;
  }
  $target_bundles = array_keys(crm_core_relationship_load_contact_types($relationship_type, $relationship_type->directional));
  if (!in_array($destination_contact->type, $target_bundles)) {
    form_set_error('destination_contact', t('Please, input destination contact of proper type.'));
    return;
  }
  if ($source_contact->contact_id == $destination_contact->contact_id) {
    form_set_error($reverse ? 'source_contact' : 'destination_contact', t('Relationship could not be loopback.'));
  }
  if ($relationship_type->r_unique) {
    if (\Drupal::service('entity.repository.relation')
      ->relationExists([
      [
        'entity_type' => 'crm_core_contact',
        'entity_id' => $source_contact->contact_id,
      ],
      [
        'entity_type' => 'crm_core_contact',
        'entity_id' => $destination_contact->contact_id,
      ],
    ], $relationship_type->relation_type)) {
      form_set_error($reverse ? 'source_contact' : 'destination_contact', t('This relationship exists. It should be unique.'));
      return;
    }
  }
  $source_contact = _crm_core_relationship_ui_get_contact_from_autocomplete_field_value($form_state['values']['source_contact']);
  $destination_contact = _crm_core_relationship_ui_get_contact_from_autocomplete_field_value($form_state['values']['destination_contact']);

  // Create relationship between the order and the node.
  $endpoints = [
    0 => [
      'entity_type' => 'crm_core_contact',
      'entity_id' => $source_contact->contact_id,
      'entity_bundle' => $source_contact->type,
      'r_index' => 0,
      'entity_key' => $source_contact->type . ': ' . $source_contact->contact_id,
    ],
    1 => [
      'entity_type' => 'crm_core_contact',
      'entity_id' => $destination_contact->contact_id,
      'entity_bundle' => $destination_contact->type,
      'r_index' => 1,
      'entity_key' => $destination_contact->type . ': ' . $destination_contact->contact_id,
    ],
  ];
  $relation = Relation::create([
    $relationship_type->relation_type,
    $endpoints,
  ]);
  $form_state['relation'] = $relation;
  field_attach_form_validate('relation', $relation, $form, $form_state);
}