2016-12-01 6 views
0

Я вижу текстовое поле и поле ckeditor на странице редактирования узла, но когда я пытаюсь сохранить узел, я получаю ошибку «Это значение должно иметь правильный примитивный тип.».Drupal 8 Custom Field // Примитивная ошибка

<?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; 
    } 

} 

Я видел, что я мог бы использовать() метод massageFormValues ​​в WidgetBase, но я не знаю, как использовать его.

Немного поможем.

Спасибо

ответ

1

, наконец я добавить в 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; 

    } 

} 

А теперь ы работает

+0

Спасибо, это работает и для меня –

0

Вы пытаетесь установить VARCHAR до 5000 размера символов, но это позволяет только 255 Ваша ошибка:

'type' => 'varchar', 
    'length' => 5000, // change it to 255 
    'size' => 'normal' // delete this line 

Если вам нужно 5000 использовать символ типа TEXT, вам не нужно для точного атрибута «размер», это только для целых

+0

Спасибо вам за ваш ответ, но у меня есть та же ошибка: s –

+0

@FrankDrebin я отредактировал мой пост – Fky

0

, кажется, из-за

$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', 
    ); 

но я не знаю, как переопределить метод массажFormValues ​​..?

+0

Добавить paranthesis условию '#default_value' => (isset ($ items [$ delta] -> programation)? $ items [$ delta] -> programation: NULL) – Fky