function paragraphs_field_widget_embed_validate in Paragraphs 7
FAPI validation of an individual paragraphs element.
Parameters
array $element: A form element array containing basic properties of the paragraphs element.
array $form_state: An associative array containing the current state of the form.
array $complete_form: The form structure array.
1 string reference to 'paragraphs_field_widget_embed_validate'
- paragraphs_field_widget_form_build in ./
paragraphs.field_widget.inc - Widget form implementation for paragraphs.
File
- ./
paragraphs.field_widget.inc, line 745 - Holds functions for the paragraphs widgets.
Code
function paragraphs_field_widget_embed_validate(array $element, array &$form_state, array $complete_form) {
$field_parents = $element['#field_parents'];
$field_name = $element['#field_name'];
$language = $element['#language'];
$field_state = field_form_get_state($field_parents, $field_name, $language, $form_state);
if (isset($field_state['entity'][$element['#delta']])) {
$paragraph_item = $field_state['entity'][$element['#delta']];
// Now validate elements if the entity is not empty.
if ((!isset($paragraph_item->removed) || !$paragraph_item->removed) && (!isset($paragraph_item->confirmed_removed) || !$paragraph_item->confirmed_removed)) {
// Attach field API validation of the embedded form.
field_attach_form_validate('paragraphs_item', $paragraph_item, $element, $form_state);
if (!empty($element['#paragraphs_required_elements'])) {
foreach ($element['#paragraphs_required_elements'] as &$elements) {
// Copied from _form_validate().
if (isset($elements['#needs_validation'])) {
$is_countable = is_array($elements['#value']) || $elements['#value'] instanceof Countable;
$is_empty_multiple = $is_countable && !count($elements['#value']);
$is_empty_string = is_string($elements['#value']) && drupal_strlen(trim($elements['#value'])) == 0;
$is_empty_value = $elements['#value'] === 0;
if ($is_empty_multiple || $is_empty_string || $is_empty_value) {
if (isset($elements['#title'])) {
$error_text = t('!name field is required.', array(
'!name' => $elements['#title'],
));
form_error($elements, filter_xss_admin($error_text));
}
else {
form_error($elements);
}
}
}
}
}
}
// Only if the form is being submitted, finish the collection entity and
// prepare it for saving.
if ($form_state['submitted']) {
$errors = form_get_errors();
if (module_exists('conditional_fields') && isset($form_state['conditional_fields_untriggered_dependents'])) {
// Conditional_fields will remove errors which occurred on "untriggered
// fields" and only remove them via the form-level validation handlers.
foreach ($form_state['conditional_fields_untriggered_dependents'] as $conditional_error) {
if (isset($conditional_error['errors'])) {
foreach ($conditional_error['errors'] as $key => $error) {
unset($errors[$key]);
}
}
}
}
if (!$errors) {
field_attach_submit('paragraphs_item', $paragraph_item, $element, $form_state);
// Load initial form values into $item, so any other form values below
// the same parents are kept.
$item = drupal_array_get_nested_value($form_state['values'], $element['#parents']);
// Set the _weight if it is a multiple field.
if (isset($element['_weight'])) {
$item['_weight'] = $element['_weight']['#value'];
}
// Put the paragraphs item in $item['entity'], so it is saved with
// the host entity via hook_field_presave() if it is not empty.
// @see paragraph_field_presave()
$item['entity'] = $paragraph_item;
form_set_value($element, $item, $form_state);
}
}
}
}