getPossibleOptions example


  public function viewElements(FieldItemListInterface $items$langcode) {
    $elements = [];

    // 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 = $options[$value] ?? $value;
        $elements[$delta] = [
          '#markup' => $output,
          '#allowed_tags' => FieldFilteredMarkup::allowedTags(),
        ];
      }
    }
'allowed_values' => [],
      'allowed_values_function' => '',
    ] + parent::defaultStorageSettings();
  }

  /** * {@inheritdoc} */
  public function getPossibleValues(AccountInterface $account = NULL) {
    // Flatten options firstly, because Possible Options may contain group     // arrays.     $flatten_options = OptGroup::flattenOptions($this->getPossibleOptions($account));
    return array_keys($flatten_options);
  }

  /** * {@inheritdoc} */
  public function getPossibleOptions(AccountInterface $account = NULL) {
    return $this->getSettableOptions($account);
  }

  /** * {@inheritdoc} */
/** * {@inheritdoc} */
  public function fieldSettingsForm(array $form, FormStateInterface $form_state) {
    $element = parent::fieldSettingsForm($form$form_state);
    $settings = $this->getSettings();

    $element['allowed_formats'] = [
      '#type' => 'checkboxes',
      '#title' => $this->t('Allowed text formats'),
      '#options' => $this->get('format')->getPossibleOptions(),
      '#default_value' => !empty($settings['allowed_formats']) ? $settings['allowed_formats'] : [],
      '#description' => $this->t('Select the allowed text formats. If no formats are selected, all available text formats will be displayed to the user.'),
      '#element_validate' => [[static::class, 'validateAllowedFormats']],
    ];

    return $element;
  }

  /** * Render API callback: Processes the allowed formats value. * * Ensure the element's value is an indexed array of selected format IDs. * This function is assigned as an #element_validate callback. * * @see static::fieldSettingsForm() */
/** * {@inheritdoc} */
  public function getSettableValues(AccountInterface $account = NULL) {
    return [0, 1];
  }

  /** * {@inheritdoc} */
  public function getSettableOptions(AccountInterface $account = NULL) {
    return $this->getPossibleOptions($account);
  }

  /** * {@inheritdoc} */
  public static function generateSampleValue(FieldDefinitionInterface $field_definition) {
    $values['value'] = mt_rand(0, 1);
    return $values;
  }

}

class FilterFormat extends StringData implements OptionsProviderInterface {

  /** * {@inheritdoc} */
  public function getPossibleValues(AccountInterface $account = NULL) {
    return array_keys($this->getPossibleOptions($account));
  }

  /** * {@inheritdoc} */
  public function getPossibleOptions(AccountInterface $account = NULL) {
    return array_map(function D$format) {
      return $format->label();
    }filter_formats());
  }

  
\Drupal::currentUser()->setAccount($user);

    $expected_available_options = [
      'filtered_html' => 'Filtered HTML',
      'full_html' => 'Full HTML',
      'filter_test' => 'Test format',
      'plain_text' => 'Plain text',
    ];

    $available_values = $data->getPossibleValues();
    $this->assertEquals(array_keys($expected_available_options)$available_values);
    $available_options = $data->getPossibleOptions();
    $this->assertEquals($expected_available_options$available_options);

    $allowed_values = $data->getSettableValues($user);
    $this->assertEquals(['plain_text']$allowed_values);
    $allowed_options = $data->getSettableOptions($user);
    $this->assertEquals(['plain_text' => 'Plain text']$allowed_options);

    $data->setValue('foo');
    $violations = $data->validate();
    $this->assertFilterFormatViolation($violations, 'foo');

    
Home | Imprint | This part of the site doesn't use cookies.