โปรดช่วยด้วยสิ่งต่อไปนี้:
ฉันต้องการวิดเจ็ตใหม่ (เพียงแค่ตัวเลือกที่เปลี่ยนแปลงเล็กน้อย) ที่ทุกคนสามารถตั้งค่าได้จาก UI ฉันได้ทำสิ่งต่อไปนี้แล้ว:
สร้างโมดูลแบบกำหนดเองใหม่และเปิดใช้งาน
 
สร้างวิดเจ็ตใหม่
 
<?php
เนมสเปซ Drupal\filtered_select\Plugin\Field\FieldWidget;
ใช้ Drupal\Core\Field\FieldItemListInterface;
ใช้ Drupal\Core\Field\Plugin\Field\FieldWidget\OptionsSelectWidget;
ใช้ Drupal\Core\Form\FormStateInterface;
/**
 * การใช้งานปลั๊กอินของวิดเจ็ต 'filtered_select'
 *
 * @ฟิลด์วิดเจ็ต(
 * id = "กรอง_select",
 * label = @Translation("เลือกตัวกรอง"),
 * field_types = {
 * "entity_reference"
 * },
 * multiple_values = FALSE
 * )
 */
คลาส FilteredSelectWidget ขยาย OptionsSelectWidget {
  /**
   * {@inheritdoc}
   */
  ฟังก์ชันสาธารณะ formElement (FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
    $element = parent::formElement($items, $delta, $element, $form, $form_state);
    // ใช้ฟังก์ชันหลักเลือก
    // เพียงแค่เปลี่ยนองค์ประกอบ
    $element['#type'] = 'fselect';
    ส่งคืนองค์ประกอบ $;
  }
}
- สร้างองค์ประกอบใหม่เพื่อให้ฉันสามารถเปลี่ยนธีมได้ (ไม่แน่ใจว่าจำเป็นไหม)
 
<?php
เนมสเปซ Drupal\filtered_select\Element;
ใช้ Drupal\Core\Render\Element\Select;
/**
 * ให้องค์ประกอบที่เลือกกรอง
 * 
 * @FormElement("fselect")
 */
class FilteredSelect ขยาย Select {
  /**
   * {@inheritdoc}
   */
  ฟังก์ชั่นสาธารณะ getInfo () {
    $info = parent::getInfo();
    // เปลี่ยนธีม
    $info['#theme'] = 'fselect';
    ส่งคืนข้อมูล $;
  }
}
- มันใช้งานไม่ได้ดังนั้นฉันจึงมี hook_theme ในไฟล์โมดูลของฉันดังนี้:
 
/**
 * ใช้ hook_theme()
 */
ฟังก์ชัน filtered_select_theme($existing, $type, $theme, $path): อาร์เรย์ {
    กลับ [
        'fselect' => [
            'องค์ประกอบการแสดงผล' => 'องค์ประกอบ',
            'เทมเพลต' => 'fselect',
        ]
    ];
}
  
- ฉันเพิ่งคัดลอกเทมเพลตจากแกนหลักไปยังโมดูลที่กำหนดเองของฉัน และมันใช้งานได้ในแง่ที่ว่าพิมพ์เทมเพลตที่ถูกต้อง ฉันยังไม่ได้แก้ไขเทมเพลตนี้ ฉันแค่ต้องการให้ฟังก์ชันหลักเดิมทำงานภายใต้ชื่ออื่นก่อน
 
ปัญหา:
องค์ประกอบการเรนเดอร์ใหม่ของฉันดูเหมือนจะไม่มีอาร์เรย์ตัวเลือก และฉันก็หาสาเหตุไม่ได้ ฉันได้เขียนทับขั้นพื้นฐานแล้วและไม่เข้าใจว่าทำไมมันไม่สร้างอาร์เรย์ตัวเลือก
ทวิก วาร์ ดัมพ์ส:
--- นี่คือตัวเลือกหลัก ---
อาร์เรย์:15 [â¼
  "องค์ประกอบ" =>อาร์เรย์:41 [â¶]
  "theme_hook_original" => "เลือก"
  "attributes" =>Drupal\Core\Template\Attribute {#7543 â¶}
  "title_attributes" =>Drupal\Core\Template\Attribute {#7932 â¶}
  "content_attributes" =>Drupal\Core\Template\Attribute {#7896 â¶}
  "title_prefix" => []
  "title_suffix" => []
  "db_is_active" =>จริง
  "is_admin" =>จริง
  "logged_in" =>จริง
  "user" =>Drupal\Core\Session\AccountProxy {#2354 â¶}
  "ไดเรกทอรี" => "หลัก/ธีม/เจ็ด"
  "ตัวเลือก" =>อาร์เรย์:3 [â¼0 =>อาร์เรย์:4 [â¶]
    1 =>อาร์เรย์:3 [â¶]
    2 =>อาร์เรย์:3 [â¶]
  ]
  "#แคช" =>อาร์เรย์:1 [â¶]
  "theme_hook_suggestions" => []
]
--- นี่คือการเลือกกรอง 'ใหม่' ของฉัน ---
อาร์เรย์:14 [â¼
  "องค์ประกอบ" =>อาร์เรย์:41 [â¶]
  "theme_hook_original" => "fselect"
  "attributes" =>Drupal\Core\Template\Attribute {#7543 â¶}
  "title_attributes" =>Drupal\Core\Template\Attribute {#8034 â¶}
  "content_attributes" =>Drupal\Core\Template\Attribute {#7708 â¶}
  "title_prefix" => []
  "title_suffix" => []
  "db_is_active" =>จริง
  "is_admin" =>จริง
  "logged_in" =>จริง
  "user" =>Drupal\Core\Session\AccountProxy {#2354 â¶}
  "ไดเรกทอรี" => "หลัก/ธีม/เจ็ด"
  "#แคช" =>อาร์เรย์:1 [â¶]
  "theme_hook_suggestions" => []
]
อย่างที่เห็น ฉันไม่มีอาร์เรย์ 'ตัวเลือก' และไม่เข้าใจว่าทำไม กรุณาแนะนำ.