Drupal 8 Custom Field // Primitive error

Frank Drebin

I see text field and ckeditor field on node edit page, but when i try to save node i receive "This value should be of the correct primitive type. " error.

    <?php
namespace Drupal\custom_field\Plugin\Field\FieldType;

use Drupal\Core\Field\FieldItemBase;
use Drupal\Core\Field\FieldItemInterface;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\TypedData\DataDefinition;

/**
 * Plugin implementation of the 'Program' field type.
 *
 * @FieldType(
 *  id = "program",
 *  label = @Translation("Programmation"),
 *  description = @Translation("Stores a Program n date string in various format"),
 *  default_widget = "program_default",
 *  default_formatter = "program_default",
 * )
 */

class ProgramItem extends FieldItemBase implements FieldItemInterface {

  public static function schema(FieldStorageDefinitionInterface $field_definition) {
    return array(
      'columns' =>  array(
        'date'  =>  array(
          'description' =>  'Programmation du jour.(date)',
          'type'  => 'varchar',
          'length'  =>  255,
          'size'  => 'normal',
        ),
        'programmation' =>  array(
          'description' =>  'Programmation. (Concerts)',
          'type'  =>  'varchar',
          'length'  =>  5000,
          'size'  =>  'normal',
        ),
      ),
    );
  }

  public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) {
    $properties['date'] = DataDefinition::create('string')
      ->setLabel(t('Date du jour'));

    $properties['programmation'] = DataDefinition::create('string')
      ->setLabel(t('Programmation du jour'));

    return $properties;
  }

  public function isEmpty() {
    $value = $this->get('date')->getValue();
    return empty($value);
  }

  public static function mainPropertyName() {
    return 'date';
  }

}



    <?php
namespace Drupal\custom_field\Plugin\Field\FieldWidget;

use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\WidgetBase;
use Drupal\Core\Field\WidgetBaseInterface;
use Drupal\Core\Form\FormStateInterface;

/**
 * Plugin implementation of the 'Program' widget.
 *
 * @FieldWidget(
 *  id = "program_default",
 *  label = @Translation("Programmation"),
 *  field_types = {
 *    "program"
 *  }
 * )
 */

class ProgramWidget extends WidgetBase implements WidgetBaseInterface {

  /**
   * @param FieldItemListInterface $items
   * @param int $delta
   * @param array $element
   * @param array $form
   * @param FormStateInterface $form_state
   * @return array
     */
  public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {

    $element['date'] = array(
      '#type' =>  'textfield',
      '#title'  =>  $this->t('Date'),
      '#placeholder'  =>  $this->getSetting('placeholder_date'),
      '#default_value' =>  isset($items[$delta]->date) ? $items[$delta]->date : NULL,
      '#required' =>  $element['#required'],
    );



    $element['programmation'] = array(
      '#type' =>  'text_format',
      '#title'  =>  $this->t('Programmation'),
      '#placeholder'  =>  $this->getSetting('placeholder_programmation'),
      '#default_value' =>  isset($items[$delta]->programmation) ? $items[$delta]->programmation : NULL,
      '#format' =>  'full_html',
    );

    $element['field_widget_display']['#access'] = true;
    $element['field_widget_display_settings']['#access'] = true;

    die('ProgramWidget');

    return $element;
  }

}

I saw that I could use the massageFormValues () method in WidgetBase, but I do not know how to use it.

A little help would be welcome.

Thank you

Frank Drebin

finaly i add to ProgramWidget :

  /**
   * {@inheritdoc}
   */
  public function massageFormValues(array $values, array $form, FormStateInterface $form_state) {

      foreach ($values as &$value) {
        if (count($value['programmation'])) {
          $value['programmation'] = $value['programmation']['value'];
        } else {
          $value['programmation'] = $value['programmation'] !== '' ? $value['programmation'] : '0';
        }
      }

    return $values;

  }

}

And now it s working

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

drupal 8 conditional field

From Dev

Drupal custom form field permission

From Dev

Fields in java, "not a primitive field" error

From Dev

ckeditor in Drupal 8 custom module

From Dev

Drupal 8 custom registration form

From Dev

Drupal 8 Custom Module Libraries

From Dev

Drupal 8 Custom form with popup

From Dev

Drupal Search API custom search field order

From Dev

Display raw text from custom field in Drupal

From Dev

Adding Drupal Field Collection element to a custom form

From Dev

Drupal Search API custom search field order

From Dev

Use VBO in Drupal to update custom user field

From Dev

Drupal 7 Persmission per custom field

From Dev

Adding elements to a field widget in Drupal 8

From Dev

Drupal 8 views alter dynamic field value

From Dev

Drupal 8 custom module add php classes

From Dev

How to get custom block content in drupal 8

From Dev

Custom 404 template file in Drupal 8

From Dev

Drupal 8 custom menu item different URI

From Dev

Drupal 7 - Fill custom form image field with image from database

From Dev

Create drupal node programatically and set custom select list field

From Dev

Extract Url & Title from link field in Drupal 8?

From Dev

How to add entity reference as a form field drupal 8? I it possible?

From Dev

how to add checkbox in field settings form in Drupal 8

From Dev

drupal 8 render date field in node twig file

From Dev

How to create Hidden type Field in Drupal 8 Entity

From Dev

Drupal 8 Remove Field labels and values from Content Type view

From Dev

How to add custom HTML on user profile page in Drupal 8?

From Dev

My Custom Block in Drupal 8 does not show up in Block Layout

Related Related

HotTag

Archive