You are here

function blockreference_autocomplete_validate in Block reference 7.2

Same name and namespace in other branches
  1. 6 blockreference.module \blockreference_autocomplete_validate()
  2. 7 blockreference.module \blockreference_autocomplete_validate()

Validation callback for a blockreference autocomplete element.

1 string reference to 'blockreference_autocomplete_validate'
blockreference_field_widget_form in ./blockreference.field.inc
Implements hook_field_widget_form().

File

./blockreference.module, line 92

Code

function blockreference_autocomplete_validate($element, &$form_state, $form) {
  $instance = field_info_instance($element['#entity_type'], $element['#field_name'], $element['#bundle']);
  $value = $element['#value'];
  if (!$value) {
    form_set_value($element, '', $form_state);
    return;
  }
  $block_arr = _blockreference_block_from_ac_string($value);

  // Validate input to be a block.
  if (!$block_arr) {
    return form_error($element, t('%name: Title mismatch. Please check your selection.', array(
      '%name' => $instance['label'],
    )));
  }

  // Validate block's usage in this context.
  $allowed_blocks = _blockreference_find_blocks($instance, '', array(
    'entity' => $element['#entity'],
    'type' => 'autocomplete',
  ));
  $moddelta = implode(':', $block_arr);
  if (!isset($allowed_blocks[$moddelta])) {
    return form_error($element, t('This block is not allowed in this field/context.'));
  }
  form_set_value($element, $moddelta, $form_state);
}