2016-12-01 26 views
0

我在節點編輯頁面上看到文本字段和ckeditor字段,但是當我嘗試保存節點時,我收到「該值應該是正確的原始類型」錯誤。Drupal 8自定義字段//原始錯誤

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

} 

我看到我可以在WidgetBase中使用massageFormValues()方法,但我不知道如何使用它。

有一點幫助是值得歡迎的。

謝謝

回答

1

finaly我添加到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; 

    } 

} 

而現在s工作

+0

謝謝,它也適用於我 –

0

您試圖設置一個varchar到5000字符的大小,但它僅允許255 你的錯誤:

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

如果您需要5000字的使用TEXT類型,你不需要精確的「大小」屬性,這只是對整數

+0

感謝您的回答,但我有同樣的錯誤:■ –

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

但我不知道如何覆蓋方法massageFormValues ..?

+0

添加paranthesis到條件'#default_value'=>(isset($ items [$ delta] - > programmation)?$ items [$ delta] - > programmation:NULL) – Fky