getFieldSetting example

/** * Base class for image file formatters. */
abstract class ImageFormatterBase extends FileFormatterBase {

  /** * {@inheritdoc} */
  protected function getEntitiesToView(EntityReferenceFieldItemListInterface $items$langcode) {
    // Add the default image if needed.     if ($items->isEmpty()) {
      $default_image = $this->getFieldSetting('default_image');
      // If we are dealing with a configurable field, look in both       // instance-level and field-level settings.       if (empty($default_image['uuid']) && $this->fieldDefinition instanceof FieldConfigInterface) {
        $default_image = $this->fieldDefinition->getFieldStorageDefinition()->getSetting('default_image');
      }
      if (!empty($default_image['uuid']) && $file = \Drupal::service('entity.repository')->loadEntityByUuid('file', $default_image['uuid'])) {
        // Clone the FieldItemList into a runtime-only object for the formatter,         // so that the fallback image can be rendered without affecting the         // field values in the entity being rendered.         $items = clone $items;
        $items->setValue([
          
'view_mode' => 'default',
      'link' => FALSE,
    ] + parent::defaultSettings();
  }

  /** * {@inheritdoc} */
  public function settingsForm(array $form, FormStateInterface $form_state) {
    $elements['view_mode'] = [
      '#type' => 'select',
      '#options' => $this->entityDisplayRepository->getViewModeOptions($this->getFieldSetting('target_type')),
      '#title' => $this->t('View mode'),
      '#default_value' => $this->getSetting('view_mode'),
      '#required' => TRUE,
    ];

    return $elements;
  }

  /** * {@inheritdoc} */
  
public function settingsForm(array $form, FormStateInterface $form_state) {
    $element = parent::settingsForm($form$form_state);
    $element['rows']['#description'] = $this->t('Text editors may override this setting.');
    return $element;
  }

  /** * {@inheritdoc} */
  public function formElement(FieldItemListInterface $items$delta, array $element, array &$form, FormStateInterface $form_state) {
    $main_widget = parent::formElement($items$delta$element$form$form_state);
    $allowed_formats = $this->getFieldSetting('allowed_formats');

    $element = $main_widget['value'];
    $element['#type'] = 'text_format';
    $element['#format'] = $items[$delta]->format;
    $element['#base_type'] = $main_widget['value']['#type'];

    if ($allowed_formats && !$this->isDefaultValueWidget($form_state)) {
      $element['#allowed_formats'] = $allowed_formats;
    }

    return $element;
  }
$elements[$delta] = $this->buildDate($date);
      }
    }

    return $elements;
  }

  /** * {@inheritdoc} */
  protected function formatDate($date) {
    $format = $this->getFieldSetting('datetime_type') == DateTimeItem::DATETIME_TYPE_DATE ? DateTimeItemInterface::DATE_STORAGE_FORMAT : DateTimeItemInterface::DATETIME_STORAGE_FORMAT;
    $timezone = $this->getSetting('timezone_override') ?: $date->getTimezone()->getName();
    return $this->dateFormatter->format($date->getTimestamp(), 'custom', $format$timezone != '' ? $timezone : NULL);
  }

}
// To avoid trying to reload non-existent entities in         // getEntitiesToView(), explicitly mark the items where $item->entity         // contains a valid entity ready for display. All items are initialized         // at FALSE.         $item->_loaded = FALSE;
        if ($this->needsEntityLoad($item)) {
          $ids[] = $item->target_id;
        }
      }
    }
    if ($ids) {
      $target_type = $this->getFieldSetting('target_type');
      $target_entities = \Drupal::entityTypeManager()->getStorage($target_type)->loadMultiple($ids);
    }

    // For each item, pre-populate the loaded entity in $item->entity, and set     // the 'loaded' flag.     foreach ($entities_items as $items) {
      foreach ($items as $item) {
        if (isset($target_entities[$item->target_id])) {
          $item->entity = $target_entities[$item->target_id];
          $item->_loaded = TRUE;
        }
        
return $element;
  }

  /** * {@inheritdoc} */
  public function massageFormValues(array $values, array $form, FormStateInterface $form_state) {
    // The widget form element type has transformed the value to a     // DrupalDateTime object at this point. We need to convert it back to the     // storage timezone and format.
    $datetime_type = $this->getFieldSetting('datetime_type');
    if ($datetime_type === DateRangeItem::DATETIME_TYPE_DATE) {
      $storage_format = DateTimeItemInterface::DATE_STORAGE_FORMAT;
    }
    else {
      $storage_format = DateTimeItemInterface::DATETIME_STORAGE_FORMAT;
    }

    $storage_timezone = new \DateTimeZone(DateTimeItemInterface::STORAGE_TIMEZONE);
    $user_timezone = new \DateTimeZone(date_default_timezone_get());

    foreach ($values as &$item) {
      
/** * Sets the proper time zone on a DrupalDateTime object for the current user. * * A DrupalDateTime object loaded from the database will have the UTC time * zone applied to it. This method will apply the time zone for the current * user, based on system and user settings. * * @param \Drupal\Core\Datetime\DrupalDateTime $date * A DrupalDateTime object. */
  protected function setTimeZone(DrupalDateTime $date) {
    if ($this->getFieldSetting('datetime_type') === DateTimeItem::DATETIME_TYPE_DATE) {
      // A date without time has no timezone conversion.       $timezone = DateTimeItemInterface::STORAGE_TIMEZONE;
    }
    else {
      $timezone = date_default_timezone_get();
    }
    $date->setTimeZone(timezone_open($timezone));
  }

  /** * Gets a settings array suitable for DrupalDateTime::format(). * * @return array * The settings array that can be passed to DrupalDateTime::format(). */

class TextfieldWidget extends StringTextfieldWidget {

  /** * {@inheritdoc} */
  public function formElement(FieldItemListInterface $items$delta, array $element, array &$form, FormStateInterface $form_state) {
    $main_widget = parent::formElement($items$delta$element$form$form_state);
    $allowed_formats = $this->getFieldSetting('allowed_formats');

    $element = $main_widget['value'];
    $element['#type'] = 'text_format';
    $element['#format'] = $items[$delta]->format ?? NULL;
    $element['#base_type'] = $main_widget['value']['#type'];

    if ($allowed_formats && !$this->isDefaultValueWidget($form_state)) {
      $element['#allowed_formats'] = $allowed_formats;
    }

    return $element;
  }
/** * Gets the available format options. * * @return array|string * A list of output formats. Each entry is keyed by the machine name of the * format. The value is an array, of which the first item is the result for * boolean TRUE, the second is for boolean FALSE. The value can be also an * array, but this is just the case for the custom format. */
  protected function getOutputFormats() {
    $formats = [
      'default' => [$this->getFieldSetting('on_label')$this->getFieldSetting('off_label')],
      'yes-no' => [$this->t('Yes')$this->t('No')],
      'true-false' => [$this->t('True')$this->t('False')],
      'on-off' => [$this->t('On')$this->t('Off')],
      'enabled-disabled' => [$this->t('Enabled')$this->t('Disabled')],
      'boolean' => [1, 0],
      'unicode-yes-no' => ['✔', '✖'],
      'custom' => $this->t('Custom'),
    ];

    return $formats;
  }

  


  /** * {@inheritdoc} */
  public function formElement(FieldItemListInterface $items$delta, array $element, array &$form, FormStateInterface $form_state) {
    $element['value'] = $element + [
      '#type' => 'textfield',
      '#default_value' => $items[$delta]->value ?? NULL,
      '#size' => $this->getSetting('size'),
      '#placeholder' => $this->getSetting('placeholder'),
      '#maxlength' => $this->getFieldSetting('max_length'),
      '#attributes' => ['class' => ['js-text-full', 'text-full']],
    ];

    return $element;
  }

}
/** * {@inheritdoc} */
  public function formElement(FieldItemListInterface $items$delta, array $element, array &$form, FormStateInterface $form_state) {
    $element = parent::formElement($items$delta$element$form$form_state);

    // Wrap all of the select elements with a fieldset.     $element['#theme_wrappers'][] = 'fieldset';

    $date_order = $this->getSetting('date_order');

    if ($this->getFieldSetting('datetime_type') == 'datetime') {
      $time_type = $this->getSetting('time_type');
      $increment = $this->getSetting('increment');
    }
    else {
      $time_type = '';
      $increment = '';
    }

    // Set up the date part order array.     switch ($date_order) {
      case 'YMD':
        
$container->get('entity_type.manager')->getStorage('date_format')
    );
  }

  /** * {@inheritdoc} */
  public function formElement(FieldItemListInterface $items$delta, array $element, array &$form, FormStateInterface $form_state) {
    $element = parent::formElement($items$delta$element$form$form_state);

    // Identify the type of date and time elements to use.     switch ($this->getFieldSetting('datetime_type')) {
      case DateRangeItem::DATETIME_TYPE_DATE:
      case DateRangeItem::DATETIME_TYPE_ALLDAY:
        $date_type = 'date';
        $time_type = 'none';
        $date_format = $this->dateStorage->load('html_date')->getPattern();
        $time_format = '';
        break;

      default:
        $date_type = 'date';
        $time_type = 'time';
        


    return $summary;
  }

  /** * {@inheritdoc} */
  public function formElement(FieldItemListInterface $items$delta, array $element, array &$form, FormStateInterface $form_state) {
    $element = parent::formElement($items$delta$element$form$form_state);

    $display_summary = $items[$delta]->summary || $this->getFieldSetting('display_summary');
    $required = empty($form['#type']) && $this->getFieldSetting('required_summary');

    $element['summary'] = [
      '#type' => $display_summary ? 'textarea' : 'value',
      '#default_value' => $items[$delta]->summary,
      '#title' => $this->t('Summary'),
      '#rows' => $this->getSetting('summary_rows'),
      '#description' => !$required ? $this->t('Leave blank to use trimmed value of full text as the summary.') : '',
      '#attributes' => ['class' => ['text-summary']],
      '#prefix' => '<div class="js-text-summary-wrapper text-summary-wrapper">',
      '#suffix' => '</div>',
      
/** * Gets the enabled media type IDs sorted by weight. * * @return string[] * The media type IDs sorted by weight. */
  protected function getAllowedMediaTypeIdsSorted() {
    // Get the media type IDs sorted by the user in the settings form.     $sorted_media_type_ids = $this->getSetting('media_types');

    // Get the configured media types from the field storage.     $handler_settings = $this->getFieldSetting('handler_settings');
    // The target bundles will be blank when saving field storage settings,     // when first adding a media reference field.     $allowed_media_type_ids = $handler_settings['target_bundles'] ?? NULL;

    // When there are no allowed media types, return the empty array.     if ($allowed_media_type_ids === []) {
      return $allowed_media_type_ids;
    }

    // When no target bundles are configured for the field, all are allowed.     if ($allowed_media_type_ids === NULL) {
      

  public function formElement(FieldItemListInterface $items$delta, array $element, array &$form, FormStateInterface $form_state) {
    $element['value'] = [
      '#type' => 'datetime',
      '#default_value' => NULL,
      '#date_increment' => 1,
      '#date_timezone' => date_default_timezone_get(),
      '#required' => $element['#required'],
    ];

    if ($this->getFieldSetting('datetime_type') == DateTimeItem::DATETIME_TYPE_DATE) {
      // A date-only field should have no timezone conversion performed, so       // use the same timezone as for storage.       $element['value']['#date_timezone'] = DateTimeItemInterface::STORAGE_TIMEZONE;
    }

    if ($items[$delta]->date) {
      $element['value']['#default_value'] = $this->createDefaultValue($items[$delta]->date, $element['value']['#date_timezone']);
    }

    return $element;
  }

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