setWidgetState example


  public function extractFormValues(FieldItemListInterface $items, array $form, FormStateInterface $form_state) {
    parent::extractFormValues($items$form$form_state);

    // Update reference to 'items' stored during upload to take into account     // changes to values like 'alt' etc.     // @see \Drupal\file\Plugin\Field\FieldWidget\FileWidget::submit()     $field_name = $this->fieldDefinition->getName();
    $field_state = static::getWidgetState($form['#parents']$field_name$form_state);
    $field_state['items'] = $items->getValue();
    static::setWidgetState($form['#parents']$field_name$form_state$field_state);
  }

  /** * Form API callback. Retrieves the value for the file_generic field element. * * This method is assigned as a #value_callback in formElement() method. */
  public static function value($element$input, FormStateInterface $form_state) {
    if ($input) {
      // Checkboxes lose their value when empty.       // If the display field is present make sure its unchecked value is saved.

  public function extractFormValues(FieldItemListInterface $items, array $form, FormStateInterface $form_state) {
    parent::extractFormValues($items$form$form_state);

    // Update reference to 'items' stored during add or remove to take into     // account changes to values like 'weight' etc.     // @see Drupal\media_library\Plugin\Field\FieldWidget\MediaLibraryWidget::addItems     // @see Drupal\media_library\Plugin\Field\FieldWidget\MediaLibraryWidget::removeItem     $field_name = $this->fieldDefinition->getName();
    $field_state = static::getWidgetState($form['#parents']$field_name$form_state);
    $field_state['items'] = $items->getValue();
    static::setWidgetState($form['#parents']$field_name$form_state$field_state);
  }

  /** * {@inheritdoc} */
  public function formElement(FieldItemListInterface $items$delta, array $element, array &$form, FormStateInterface $form_state) {
    /** @var \Drupal\Core\Field\EntityReferenceFieldItemListInterface $items */
    $referenced_entities = $items->referencedEntities();
    $view_builder = $this->entityTypeManager->getViewBuilder('media');
    $field_name = $this->fieldDefinition->getName();
    $parents = $form['#parents'];
    

  public function form(FieldItemListInterface $items, array &$form, FormStateInterface $form_state$get_delta = NULL) {
    $field_name = $this->fieldDefinition->getName();
    $parents = $form['#parents'];

    // Store field information in $form_state.     if (!static::getWidgetState($parents$field_name$form_state)) {
      $field_state = [
        'items_count' => count($items),
        'array_parents' => [],
      ];
      static::setWidgetState($parents$field_name$form_state$field_state);
    }

    // Collect widget elements.     $elements = [];

    // If the widget is handling multiple values (e.g Options), or if we are     // displaying an individual element, just get a single form element and make     // it the $delta value.     if ($this->handlesMultipleValues() || isset($get_delta)) {
      $delta = $get_delta ?? 0;
      $element = [
        
Home | Imprint | This part of the site doesn't use cookies.