You are here

function ctools_context_entity_settings_form_validate in Chaos Tool Suite (ctools) 7

Validate a node.

File

plugins/contexts/entity.inc, line 162
Plugin to provide a node context. A node context is a node wrapped in a context object that can be utilized by anything that accepts contexts.

Code

function ctools_context_entity_settings_form_validate($form, &$form_state) {

  // Validate the autocomplete.
  if (empty($form_state['values']['entity_id']) && empty($form_state['values']['entity'])) {
    form_error($form['entity'], t('You must select an entity.'));
    return;
  }
  if (empty($form_state['values']['entity'])) {
    return;
  }
  $id = $form_state['values']['entity'];
  $preg_matches = array();
  $match = preg_match('/\\[id: (\\d+)\\]/', $id, $preg_matches);
  if (!$match) {
    $match = preg_match('/^id: (\\d+)/', $id, $preg_matches);
  }
  if ($match) {
    $id = $preg_matches[1];
  }
  if (is_numeric($id)) {
    $entity = entity_load($form_state['values']['entity_type'], array(
      $id,
    ));
    $entity = $entity[$id];
  }
  else {
    $entity_info = entity_get_info($form_state['values']['entity_type']);
    $field = $entity_info['entity keys']['label'];
    $entity = entity_load($form_state['values']['entity_type'], FALSE, array(
      $field => $id,
    ));
  }

  // Do not allow unpublished nodes to be selected by unprivileged users
  // || (empty($node->status) && !(user_access('administer nodes'))) need a new sanity check at some point.
  if (!$entity) {
    form_error($form['entity'], t('Invalid entity selected.'));
  }
  else {
    $entity_id = entity_extract_ids($form_state['values']['entity_type'], $entity);
    form_set_value($form['entity_id'], $entity_id[0], $form_state);
  }
}