Score:1

How do I add a template suggestion for form input fields that have containers?

ธง in

I've been able to successfully add the parent form ID to all form elements, based on these two questions: How can we find the id of the parent form in preprocess_HOOK() or HOOK_alter() theme functions? and Add the Form name to the theme suggestions input field.

However, the two elements I really need template suggestions for are inputs that have containers... therefore the parent form ID is added to the containers, not the inputs, and when I add the template suggestions the variable is blank (obviously).

I see there are modules like Formdazzle! which makes this easy, but I feel like I'm close - however I'm struggling to 'reach' the children (the inputs) of the form elements (the containers).

This is my current code:

 * Implements hook_form_alter().
 */
function HOOK_form_alter(&$form, FormStateInterface $form_state, $form_id) {

  foreach (\Drupal\Core\Render\Element::children($form) as $key) {
    if ($form[$key]['#parents']) {
      $form[$key]['#attributes']['data-parent-form'] = $form['#id'];
    } else {
      // help!
    }
  }
}

/**
 * Implements hook_theme_suggestions_input_alter().
 */
function HOOK_theme_suggestions_input_alter(&$suggestions, array $variables) {
  $element = $variables['element'];
  $suggestions[] = 'input__' . $element['#type'] . '__' . $element['#attributes']['data-parent-form'];
}
leymannx avatar
ne flag
คุณสามารถประมวลผลล่วงหน้าการตรวจสอบคอนเทนเนอร์สำหรับอินพุตของคุณในนั้น และหากคุณพบ ให้กำหนดตัวแปรให้กับอินพุตจากในนั้น ตัวแปรนี้สามารถเข้าถึงได้ในตัวแปรขอคำแนะนำในภายหลัง
in flag
@leymannx ฉันตรวจสอบสิ่งนี้และลงเอยด้วย Formdazzle - ฉันพยายามสร้างวงล้อใหม่และท้ายที่สุดก็จำเป็นต้องประหยัดเวลา (และกังวลว่าสถานการณ์อื่นจะไม่ครอบคลุมในอนาคต) นี่เป็นวิธีดำเนินการต่อไปอย่างแน่นอน
leymannx avatar
ne flag
ใช่ [Formdazzle](https://www.drupal.org/project/formdazzle) ดีเสมอสำหรับการกำหนดรูปแบบ อาจเพิ่มว่าเป็นคำตอบด้านล่าง!

โพสต์คำตอบ

คนส่วนใหญ่ไม่เข้าใจว่าการถามคำถามมากมายจะปลดล็อกการเรียนรู้และปรับปรุงความสัมพันธ์ระหว่างบุคคล ตัวอย่างเช่น ในการศึกษาของ Alison แม้ว่าผู้คนจะจำได้อย่างแม่นยำว่ามีคำถามกี่ข้อที่ถูกถามในการสนทนา แต่พวกเขาไม่เข้าใจความเชื่อมโยงระหว่างคำถามและความชอบ จากการศึกษาทั้ง 4 เรื่องที่ผู้เข้าร่วมมีส่วนร่วมในการสนทนาด้วยตนเองหรืออ่านบันทึกการสนทนาของผู้อื่น ผู้คนมักไม่ตระหนักว่าการถามคำถามจะมีอิทธิพลหรือมีอิทธิพลต่อระดับมิตรภาพระหว่างผู้สนทนา