/** * {@inheritdoc} */ public function getSettableValues(AccountInterface $account = NULL) { // Flatten options firstly, because Settable Options may contain group // arrays. $flatten_options = OptGroup::flattenOptions($this->getSettableOptions($account)); return array_keys($flatten_options); }
/** * {@inheritdoc} */ public function getSettableValues(AccountInterface $account = NULL) { // Flatten options firstly, because Settable Options may contain group // arrays. $values = array_keys(OptGroup::flattenOptions($this->getSettableOptions($account))); $values[] = static::$NEW_ENTITY_MARKER; return $values; }
/** * {@inheritdoc} */ protected function getSelectedOptions(FieldItemListInterface $items, $delta = 0) { // Copy parent behavior but also check the status property. $flat_options = OptGroup::flattenOptions($this->getOptions($items->getEntity())); $selected_options = array(); foreach ($items as $item) { $value = $item->{$this->column}; // Keep the value if it actually is in the list of options (needs to be // checked against the flat list). if ($item->status == SIMPLENEWS_SUBSCRIPTION_STATUS_SUBSCRIBED && isset($flat_options[$value])) { $selected_options[] = $value; } } return $selected_options; }
/** * {@inheritdoc} */ public function viewElements(FieldItemListInterface $items, $langcode) { $elements = array(); // Only collect allowed options if there are actually items to display. if ($items->count()) { $provider = $items->getFieldDefinition()->getFieldStorageDefinition()->getOptionsProvider('value', $items->getEntity()); // Flatten the possible options, to support opt groups. $options = OptGroup::flattenOptions($provider->getPossibleOptions()); foreach ($items as $delta => $item) { $value = $item->value; // If the stored value is in the current set of allowed values, display // the associated label, otherwise just display the raw value. $output = isset($options[$value]) ? $options[$value] : $value; $elements[$delta] = array('#markup' => $output, '#allowed_tags' => FieldFilteredMarkup::allowedTags()); } } return $elements; }
/** * Determines selected options from the incoming field values. * * @param \Drupal\Core\Field\FieldItemListInterface $items * The field values. * * @return array * The array of corresponding selected options. */ protected function getSelectedOptions(FieldItemListInterface $items) { // We need to check against a flat list of options. $flat_options = OptGroup::flattenOptions($this->getOptions($items->getEntity())); $selected_options = array(); foreach ($items as $item) { $value = $item->{$this->column}; // Keep the value if it actually is in the list of options (needs to be // checked against the flat list). if (isset($flat_options[$value])) { $selected_options[] = $value; } } return $selected_options; }
/** * Performs validation of elements that are not subject to limited validation. * * @param array $elements * An associative array containing the structure of the form. * @param \Drupal\Core\Form\FormStateInterface $form_state * The current state of the form. The current user-submitted data is stored * in $form_state->getValues(), though form validation functions are passed * an explicit copy of the values for the sake of simplicity. Validation * handlers can also $form_state to pass information on to submit handlers. * For example: * $form_state->set('data_for_submission', $data); * This technique is useful when validation requires file parsing, * web service requests, or other expensive requests that should * not be repeated in the submission step. */ protected function performRequiredValidation(&$elements, FormStateInterface &$form_state) { // Verify that the value is not longer than #maxlength. if (isset($elements['#maxlength']) && Unicode::strlen($elements['#value']) > $elements['#maxlength']) { $form_state->setError($elements, $this->t('@name cannot be longer than %max characters but is currently %length characters long.', array('@name' => empty($elements['#title']) ? $elements['#parents'][0] : $elements['#title'], '%max' => $elements['#maxlength'], '%length' => Unicode::strlen($elements['#value'])))); } if (isset($elements['#options']) && isset($elements['#value'])) { if ($elements['#type'] == 'select') { $options = OptGroup::flattenOptions($elements['#options']); } else { $options = $elements['#options']; } if (is_array($elements['#value'])) { $value = in_array($elements['#type'], array('checkboxes', 'tableselect')) ? array_keys($elements['#value']) : $elements['#value']; foreach ($value as $v) { if (!isset($options[$v])) { $form_state->setError($elements, $this->t('An illegal choice has been detected. Please contact the site administrator.')); $this->logger->error('Illegal choice %choice in %name element.', array('%choice' => $v, '%name' => empty($elements['#title']) ? $elements['#parents'][0] : $elements['#title'])); } } } elseif ($elements['#type'] == 'select' && !$elements['#multiple'] && $elements['#required'] && !isset($elements['#default_value']) && $elements['#value'] === $elements['#empty_value']) { $elements['#value'] = NULL; $form_state->setValueForElement($elements, NULL); } elseif (!isset($options[$elements['#value']])) { $form_state->setError($elements, $this->t('An illegal choice has been detected. Please contact the site administrator.')); $this->logger->error('Illegal choice %choice in %name element.', array('%choice' => $elements['#value'], '%name' => empty($elements['#title']) ? $elements['#parents'][0] : $elements['#title'])); } } }
/** * {@inheritdoc} */ public function fieldSettingsForm(array $form, FormStateInterface $form_state) { $element = parent::fieldSettingsForm($form, $form_state); $mc_list_id = $this->getFieldDefinition()->getSetting('mc_list_id'); if (empty($mc_list_id)) { drupal_set_message(t('Select a list to sync with on the Field Settings tab before configuring the field instance.'), 'error'); return $element; } $this->definition; $instance_settings = $this->definition->getSettings(); $element['show_interest_groups'] = array('#title' => "Enable Interest Groups", '#type' => "checkbox", '#default_value' => $instance_settings['show_interest_groups']); $element['interest_groups_label'] = array('#title' => "Interest Groups Label", '#type' => "textfield", '#default_value' => !empty($instance_settings['show_interest_groups']) ? $instance_settings['show_interest_groups'] : 'Interest Groups'); $element['merge_fields'] = array('#type' => 'fieldset', '#title' => t('Merge Fields'), '#description' => t('Multi-value fields will only sync their first value to Mailchimp, as Mailchimp does not support multi-value fields.'), '#tree' => TRUE); $element['unsubscribe_on_delete'] = array('#title' => "Unsubscribe on deletion", '#type' => "checkbox", '#description' => t('Unsubscribe entities from this list when they are deleted.'), '#default_value' => $instance_settings['unsubscribe_on_delete']); $mv_defaults = $instance_settings['merge_fields']; $mergevars = mailchimp_get_mergevars(array($mc_list_id)); $fields = $this->getFieldmapOptions($this->getFieldDefinition()->entity_type, $this->getFieldDefinition()->bundle); $required_fields = $this->getFieldmapOptions($this->getFieldDefinition()->entity_type, $this->getFieldDefinition()->bundle, TRUE); // Prevent this subscription field appearing as a merge field option. $field_name = $this->getFieldDefinition()->getName(); unset($fields[$field_name]); $fields_flat = OptGroup::flattenOptions($fields); foreach ($mergevars[$mc_list_id]['merge_vars'] as $mergevar) { $default_value = isset($mv_defaults[$mergevar['tag']]) ? $mv_defaults[$mergevar['tag']] : -1; $element['merge_fields'][$mergevar['tag']] = array('#type' => 'select', '#title' => SafeMarkup::checkPlain($mergevar['name']), '#default_value' => array_key_exists($default_value, $fields_flat) ? $default_value : '', '#required' => $mergevar['req']); if (!$mergevar['req'] || $mergevar['tag'] === 'EMAIL') { $element['merge_fields'][$mergevar['tag']]['#options'] = $fields; if ($mergevar['tag'] === 'EMAIL') { $element['merge_fields'][$mergevar['tag']]['#description'] = t('Any entity with an empty or invalid email address field value will simply be ignored by the Mailchimp subscription system. <em>This is why the Email field is the only required merge field which can sync to non-required fields.</em>'); } } else { $element['merge_fields'][$mergevar['tag']]['#options'] = $required_fields; $element['merge_fields'][$mergevar['tag']]['#description'] = t("Only 'required' and 'calculated' fields are allowed to be synced with Mailchimp 'required' merge fields."); } } return $element; }
public function validate() { $this->getValueOptions(); $errors = parent::validate(); // If the operator is an operator which doesn't require a value, there is // no need for additional validation. if (in_array($this->operator, $this->operatorValues(0))) { return array(); } if (!in_array($this->operator, $this->operatorValues(1))) { $errors[] = $this->t('The operator is invalid on filter: @filter.', array('@filter' => $this->adminLabel(TRUE))); } if (is_array($this->value)) { if (!isset($this->valueOptions)) { // Don't validate if there are none value options provided, for example for special handlers. return $errors; } if ($this->options['exposed'] && !$this->options['expose']['required'] && empty($this->value)) { // Don't validate if the field is exposed and no default value is provided. return $errors; } // Some filter_in_operator usage uses optgroups forms, so flatten it. $flat_options = OptGroup::flattenOptions($this->valueOptions); // Remove every element which is not known. foreach ($this->value as $value) { if (!isset($flat_options[$value])) { unset($this->value[$value]); } } // Choose different kind of output for 0, a single and multiple values. if (count($this->value) == 0) { $errors[] = $this->t('No valid values found on filter: @filter.', array('@filter' => $this->adminLabel(TRUE))); } } elseif (!empty($this->value) && ($this->operator == 'in' || $this->operator == 'not in')) { $errors[] = $this->t('The value @value is not an array for @operator on filter: @filter', array('@value' => var_export($this->value), '@operator' => $this->operator, '@filter' => $this->adminLabel(TRUE))); } return $errors; }
/** * Tests the flattenOptions() method. * * @dataProvider providerTestFlattenOptions */ public function testFlattenOptions($options) { $this->assertSame(array('foo' => 'foo'), OptGroup::flattenOptions($options)); }