2016-08-17 14 views
1

私は、引用、名前、会社、およびオプションのヘッドショットを含む新しいフィールドタイプを作成しています。Drupal 8フィールドプラグインフィールドタイプmanaged_file

私のコードは

modules/mymodule/src/FieldType/Testimonial.php 

<?php 

namespace Drupal\dods\Plugin\Field\FieldType; 

use Drupal\Core\Field\FieldItemBase; 
use Drupal\Core\TypedData\DataDefinition; 
use Drupal\Core\Field\FieldStorageDefinitionInterface as StorageDefinition; 

/** 
* Plugin implementation of the 'Testimonial' field type. 
* 
* @FieldType(
* id = "Testimonial", 
* label = @Translation("Testimonial"), 
* description = @Translation("Stores Testimonial Information."), 
* category = @Translation("Custom"), 
* default_widget = "TestimonialDefaultWidget", 
* default_formatter = "TestimonialDefaultFormatter" 
*) 
* 
* Class Testimonial 
* @package Drupal\dods\Plugin\Field\FieldType 
*/ 
class Testimonial extends FieldItemBase 
{ 

/** 
* Field type properties definition. 
* 
* Inside this method we defines all the fields (properties) that our 
* custom field type will have. 
* 
* @param StorageDefinition $storage 
* @return array 
*/ 
public static function propertyDefinitions(StorageDefinition $storage) 
{ 
    $properties['testimonial_quote'] = DataDefinition::create('string')->setLabel(t('Quote')); 
    $properties['testimonial_name'] = DataDefinition::create('string')->setLabel(t('Name')); 
    $properties['testimonial_company'] = DataDefinition::create('string')->setLabel(t('Company')); 
    $properties['testimonial_headshot'] = DataDefinition::create('integer')->setLabel(t('Headshot')); 

    return $properties; 
} 

/** 
* Field type schema definition. 
* 
* Inside this method we defines the database schema used to store data for 
* our field type. 
* @param StorageDefinition $storage 
* @return array 
*/ 
public static function schema(StorageDefinition $storage) 
{ 
    $schema['columns']['testimonial_quote'] = ['type' => 'text']; 
    $schema['columns']['testimonial_name'] = ['type' => 'char', 'length' => 100]; 
    $schema['columns']['testimonial_company'] = ['type' => 'char', 'length' => 100]; 
    $schema['columns']['testimonial_headshot'] = ['type' => 'int']; 

    return $schema; 
} 

/** 
* Define when the field type is empty. 
* 
* This method is important and used internally by Drupal. Take a moment 
* to define when the field type must be considered empty. 
* 
* @return bool 
*/ 
public function isEmpty() 
{ 
    return empty($this->get('testimonial_quote')->getValue()) && empty($this->get('testimonial_name')->getValue()) && empty($this->get('testimonial_company')->getValue()); 
} 

} 

を下回っていると私のウィジェットは、この

modules/mymodule/src/Plugin/Field/FieldWidget/TestimonialDefaultWidget.php 

<?php 

namespace Drupal\dods\Plugin\Field\FieldWidget; 

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

/** 
* Plugin implementation of the 'TestimonialDefaultWidget' widget. 
* 
* @FieldWidget(
* id = "TestimonialDefaultWidget", 
* label = @Translation("Testimonial Information"), 
* field_types = { 
*  "Testimonial" 
* } 
*) 
* 
* Class TestimonialDefaultWidget 
* @package Drupal\dods\Plugin\Field\FieldWidget 
*/ 
class TestimonialDefaultWidget extends WidgetBase 
{ 

/** 
* Define the form for the field type. 
* 
* Inside this method we can define the form used to edit the field type. 
* 
* @param FieldItemListInterface $items 
* @param int $delta 
* @param array $element 
* @param array $form 
* @param FormStateInterface $formState 
* @return array 
*/ 
public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $formState) 
{ 
    $element['testimonial_quote'] = [ 
     '#type' => 'textarea', 
     '#title' => t('Quote'), 
     '#default_value' => isset($items[$delta]->testimonial_quote) ? $items[$delta]->testimonial_quote : null, 
     '#empty_value' => '', 
     '#placeholder' => t('Quote'), 
    ]; 
    $element['testimonial_name'] = [ 
     '#type' => 'textfield', 
     '#title' => t('Name'), 
     '#default_value' => isset($items[$delta]->testimonial_name) ? $items[$delta]->testimonial_name : null, 
     '#empty_value' => '', 
     '#placeholder' => t('Name'), 
    ]; 
    $element['testimonial_company'] = [ 
     '#type' => 'textfield', 
     '#title' => t('Company'), 
     '#default_value' => isset($items[$delta]->testimonial_company) ? $items[$delta]->testimonial_company : null, 
     '#empty_value' => '', 
     '#placeholder' => t('Company'), 
    ]; 

    $element['testimonial_headshot'] = array(
     '#type' => 'managed_file', 
     '#upload_validators' => array(
      'file_validate_extensions' => array('gif png jpg jpeg'), 
      'file_validate_size' => array(25600000), 
     ), 
     '#required' => false 
    ); 

    return $element; 
} 

} 

それはすべての罰金looksdが、私が何かを保存しようと、これまで私は、このエラー

にこの値を取得するときのように見えます正しいプリミティブ型でなければなりません。

すべてのアイデアは、私はレンガの壁に当たっていいただければ幸いです:(

UPDATE

は少しハック感じが、これはあまりにも、私は、次の

をしなければならなかった仕事を取得します

フォームに追加します。

$element['testimonial_headshot_revision'] = [ 
     '#type' => 'hidden', 
     '#default_value' => isset($items[$delta]->testimonial_headshot) ? $items[$delta]->testimonial_headshot : null 
    ]; 

新しい機能で追加するあなたはintとしてtestimonial_headshotを定義しますが、ファイルのアップロードウィジェットを提供しているためであるので、

/** 
* {@inheritdoc} 
*/ 
public function massageFormValues(array $values, array $form, FormStateInterface $form_state) 
{ 
    foreach ($values as &$value) { 
     if (count($value['testimonial_headshot'])) { 
      foreach ($value['testimonial_headshot'] as $fid) { 
       $value['testimonial_headshot'] = $fid; 
      } 
     } else { 
      $value['testimonial_headshot'] = $value['testimonial_headshot_revision'] !== '' ? $value['testimonial_headshot_revision'] : '0'; 
     } 

    } 

    return $values; 
} 
+0

同じ問題が発生しました。 – JayKandari

答えて

0

などのnは、同じファイル。一般的なヒントとして:あなたが "新しい"ものをプログラムするたびに、あなたがやろうとしているものに似たものを見つけて、それを拡張/変更できるかどうかを見てください。

例: 私にとっては、新しいフィールドは基本的にいくつかの余分なテキストフィールドを持つイメージフィールドです。その後、

class TestimonialItem extends ImageItem 

そして:

public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) { 
    // get the image item properties 
    $properties = parent::propertyDefinitions($field_definition); 

    // if you don't need some properties of the parent do this: 
    unset($properties['some_property_of_parent']); 

    // add your thing 
    $properties['testimonial_quote'] = DataDefinition::create('string') 
     ->setLabel(t('Quote')) 
     ->setDescription(t("This is the quote of the testimonial")); 

    // etc. 

    return $properties; 
} 

をだからあなたは何ができるかだからあなたの新しいフィールドタイプは、次のようになりますImageItemのフィールドタイプを見て、testimonial_quoteなど

でそれを拡張していますこれは主なアイデアです。schema()メソッドとウィジェットクラスについても同様です。親をつかみ、それを伸ばす。

これが役に立ちます。

+0

こんにちは、実際には3つのフィールドと画像フィールドがありますが、これは繰り返しも可能です。現在、ImageItemクラスは、画像がアップロードされたときに、希望の結果ではなく、画像フィールド内でこれらのフィールドを繰り返します。 基本的には、フィールド1、フィールド2、フィールド3、イメージ{詳細ボタンを追加} –

+0

不要な親のプロパティを設定解除することができます。このように:unset($ properties ['something']); ImageItem自体は、FileItemを拡張するので、これを行います。 –

+0

ありがとうフランク私はこれを行う –