สิ่งที่ฉันต้องการจะทำ
ฉันต้องการทราบสิ่งนี้ใน Drupal9
- สุดท้าย ฉันต้องการสร้างฟิลด์ที่กำหนดเองบนเว็บฟอร์ม
 
- ด้วยฟิลด์ที่กำหนดเองนี้ ผู้ส่งที่ใช้เว็บฟอร์มสามารถเพิ่มคำศัพท์อนุกรมวิธานใหม่ให้กับคำศัพท์ (คำศัพท์นี้กำหนดไว้ล่วงหน้าในโมดูล) บนเว็บฟอร์มนี้
 
- ช่องนี้เป็นช่องข้อความ
 
- หากคำศัพท์ใหม่ออกจากคำศัพท์แล้ว จะไม่เพิ่มคำศัพท์นั้นลงในคำศัพท์
 
- หากเป็นไปได้ ผู้ส่งสามารถเลือกข้อกำหนดจากข้อกำหนดที่ได้ลงทะเบียนไว้แล้ว
 
ปัญหา
ฉันกำลังลองใช้ฟิลด์ที่กำหนดเองซึ่งสามารถลงทะเบียนคำศัพท์ในประเภทเนื้อหาได้
(ฉันไม่สามารถเพิ่มฟิลด์ที่กำหนดเองลงในเว็บฟอร์มได้ ดังนั้นฉันจึงลองก่อน)
ฉันสร้างมันอย่างไม่แน่นอนแล้ว อย่างไรก็ตาม ถ้าฉันเขียนบางอย่างลงในฟิลด์นี้ คำเตือนจะถูกส่งออกไป
คำเตือน: การแปลงอาร์เรย์เป็นสตริงใน Drupal\mymodule2\Plugin\Field\FieldType\MyModule2Field->isEmpty() (บรรทัดที่ 62 ของ modules/custom/mymodule2/src/Plugin/Field/FieldType/MyModule2Field.php)
MyModule2 เป็นโมดูลแบบกำหนดเองที่ฉันสร้างขึ้น
ดังนั้นฉันต้องการทราบวิธีสร้างฟิลด์ที่กำหนดเองซึ่งสามารถลงทะเบียนคำศัพท์ได้อย่างถูกต้อง
โครงสร้างโค้ดและโฟลเดอร์
mymodule2 (โฟลเดอร์)
-mymodule2.info.yml
-mymodule2.module
-src(โฟลเดอร์)/ปลั๊กอิน(โฟลเดอร์)/ฟิลด์(โฟลเดอร์)
ฟิลด์(โฟลเดอร์)-FieldType(โฟลเดอร์)-MyModule2Field.php
ฟิลด์(โฟลเดอร์)-FieldWidget(โฟลเดอร์)-MyModule2Widget.php
ฟิลด์ (โฟลเดอร์) -FieldFormatter (โฟลเดอร์) -MyModule2Formatter.php
รหัส
mymodule2.info.yml
ชื่อ: mymodule2
ประเภท: โมดูล
คำอธิบาย : hogehoge
core_version_requirement: ^8.8 || ^9
แพ็คเกจ: ตัวอย่าง
mymodule2.module
<?php ?>
MyModule2Field.php
    <?php 
namespace Drupal\mymodule2\Plugin\Field\FieldType;
use Drupal\Core\Field\FieldItemBase;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\TypedData\DataDefinition;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\taxonomy\Entity\Term;      
use Drupal\Core\Link;
use Drupal\Core\Url;
/**
 * Plugin implementation of the 'MyModule2' field type.
 *
 * @FieldType(
 *   id = "mymodule2",
 *   label = @Translation("MyModule"),
 *   description = @Translation("This field is used to store alpha-numeric values."),
 *   default_widget = "MyModule2Widget",
 *   default_formatter = "MyModule2Formatter"
 * )
 */
class MyModule2Field extends FieldItemBase {
 
 /**
  * {@inheritdoc}
  */
  
  public static function propertyDefinitions(FieldStorageDefinitionInterface $definition) {
    // Prevent early t() calls by using the TranslatableMarkup.
    $properties['mymodule2'] = DataDefinition::create('string')
    ->setLabel(new TranslatableMarkup('Text value'));
 
    return $properties;
  }
 
  /**
   * {@inheritdoc}
   */
  public static function schema(FieldStorageDefinitionInterface $definition) {
    $schema = [
        'columns' => [
          'mymodule2' => [
            'type' => 'varchar',
            'length' => 255,
          ],
        ],
    ];
    
    
    
    return $schema;
  }
 
  /**
   * {@inheritdoc}
   */
  public function isEmpty() {
    $value = $this->getValue();
    if (isset($value['mymodule2']) && $value['mymodule2'] != '') {
      
      /*this is problem code*/
      $val=implode("",$value);
      $arr = explode(',',$val);
      $categories_vocabulary='pixelarttag';
      foreach($arr as $a){
        $term = term::create(array(
        'parent' => array(),
        'name' => $a,
        'vid' => $categories_vocabulary,))->save();
      }
      return FALSE;
    }
    return TRUE;
  }
 
}
?>
MyModule2Widget.php
<?php 
namespace Drupal\mymodule2\Plugin\Field\FieldWidget;
 
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\WidgetBase;
use Drupal\Core\Form\FormStateInterface;
 
/**
 * Plugin implementation of the 'MyModule2Widget' widget.
 *
 * @FieldWidget(
 *   id = "MyModule2Widget",
 *   label = @Translation("My Field widget"),
 *   field_types = {
 *     "mymodule2"
 *   }
 * )
 */
class MyModule2Widget extends WidgetBase {
 
 /**
   * {@inheritdoc}
   */
  public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
   
    $element['mymodule2'] =  [
      '#type' => 'textfield',
      '#title' => 'AddictionTaxonomy',
      '#description' => 'Custom field to be used for alpha-numeric values',
      '#default_value' => isset($items[$delta]->title) ? $items[$delta]->title : NULL,
      '#weight' => 0,
    ];
 
    return $element;
  }
 
}
?>
MyModule2Formatter.php
<?php
namespace Drupal\mymodule2\Plugin\Field\FieldFormatter;
 
use Drupal\Core\Field\FormatterBase;
use Drupal\Core\Field\FieldItemListInterface;
 
/**
 * Plugin implementation of the 'MyModule2Formatter' formatter.
 *
 * @FieldFormatter(
 *   id = "MyModule2Formatter",
 *   label = @Translation("My Field Formatter"),
 *   field_types = {
 *     "mymodule2"
 *   }
 * )
 */
class MyModule2Formatter extends FormatterBase {
 
 /**
   * {@inheritdoc}
   */
  public static function defaultSettings() {
    return [
      // Implement default settings.
    ] + parent::defaultSettings();
  }
 
  /**
   * {@inheritdoc}
   */
  public function settingsSummary() {
    $summary = [];
    $summary[] = $this->t('Displays the random string.');
    return $summary;
  }
 
  /**
   * {@inheritdoc}
   */
  public function viewElements(FieldItemListInterface $items, $langcode) {
 
    $elements = [];
    foreach ($items as $delta => $item) {
      $elements[$delta] = [
        '#type' => 'markup',
        '#markup' => $item->mymodule2
      ];
    }
 
    return $elements;
  }
}
?>
ป.ล.
ฉันไม่เก่งภาษาอังกฤษ และฉันก็ไม่ค่อยรู้เกี่ยวกับ Drupal มากนัก ฉันเรียน Drupal ตั้งแต่ 1 เดือนที่แล้ว ดังนั้นฉันอาจถามอะไรโง่ๆ ได้โปรดยกโทษให้ฉันด้วย
ขอแสดงความนับถือ