ฉันจะเรียนรู้เกี่ยวกับวิธีสร้างตัวกรองแบบเปิดเผยที่กำหนดเองสำหรับ Views 3 และ D7 ได้อย่างไร


18

ฉันกำลังพยายามเรียนรู้วิธีสร้างตัวกรองที่เปิดเผยแบบกำหนดเองสำหรับโมดูลที่มีส่วนร่วม (เลือกหรืออื่น ๆ ) ฉันพบบทช่วยสอนนี้สำหรับ Drupal 6 แต่โค้ดไม่ปรากฏว่าทำงานนอกกรอบบน Drupal 7

ฉันยังลองดูรหัสในโมดูลเลือกแบบลำดับชั้นแต่ดูเหมือนจะซับซ้อนกว่าสิ่งที่ฉันพยายามทำ

ไม่มีใครมีข้อเสนอแนะสำหรับการสอนหรือโมดูลที่ใช้ตัวกรองสัมผัสที่กำหนดเองในวิธีที่ค่อนข้างง่าย (เช่นไม่มีตัวจัดการที่กำหนดเองจำนวนมากเช่นโมดูลตำแหน่ง) ที่ฉันสามารถเรียนรู้ได้หรือไม่

คำตอบ:


6

คำตอบสั้น ๆ : ไม่มีที่ไหนเลย

แต่คุณสามารถหาข้อมูลได้ที่นี่และที่นั่น

สถานที่แรกที่ต้องดูคือในแหล่งที่มาของ Views โดยเฉพาะการใช้งานตัวกรองที่มีอยู่เริ่มต้นด้วยตัวกรองที่ง่ายกว่า

Personnaly ฉันเข้าร่วมในหัวข้อนี้ซึ่งเป็นคำแนะนำที่ดี แต่ไม่พอใจอย่างสมบูรณ์ตามที่คุณจะเห็นว่าคุณไปอ่านมัน ฉันคิดว่าข้อมูลที่นี่มีประโยชน์และอย่างน้อยฉันก็สามารถพูดได้ว่ามันเป็น acurate ในเวลาที่มันถูกเขียน


2
นี่คือการประชุมจาก Drupalcon ครั้งล่าสุดที่ฉันเพิ่งค้นพบและรวมถึงข้อมูลที่มีประโยชน์อย่างไม่น่าเชื่อรวมถึงการกล่าวถึงส่วนของ Views ของเอกสารapi.drupal.org ที่ฉันไม่รู้ นี่เป็นจุดเริ่มต้นที่ดีที่สุดสำหรับการพัฒนา Views ที่ฉันรู้จัก
Countzero

10

ฉันซุ่มซ่อนรอบอินเทอร์เน็ตพยายามหาคำตอบสำหรับคำถามเดียวกันและนี่คือสิ่งที่ฉันได้รับ:

  1. ใช้ hooks หลายอันในโมดูลที่คุณกำหนดเอง แทนที่modulenameและfilternameมีชื่อที่แท้จริงของคุณ

    /**
     * Implements hook_views_api().
     */
    function modulename_views_api() {
      return array(
        'api' => 2,
        'path' => drupal_get_path('module', 'modulename') . '/inc',
      );
    }
    
    /**
     * Implementation of hook_views_handlers() to register all of the basic handlers
     * views uses.
     */
    function modulename_views_handlers() {
      return array(
        'info' => array(
          // path to handler files
          'path' => drupal_get_path('module', 'modulename') . '/inc',
        ),
        'handlers' => array(
          // register our custom filter, with the class/file name and parent class
          'modulename_handler_filter_filtername' => array(
            'parent' => 'views_handler_filter',
          ),
        ),
      );
    }
    
    function modulename_views_data() {
      $data = array();
    
      $data['node']['filtername'] = array(
        'group' => t('Custom'),
        'real field' => 'my_custom_filter_field',
        'title' => t('My custom filter'),
        'help' => t('Some more detailed description if you need it.'),
        'filter' => array(
          'handler' => 'modulename_handler_filter_filtername',
        ),
      );
    
      return $data;
    }
    
  2. สร้างโฟลเดอร์ที่มีชื่อincอยู่ในโฟลเดอร์โมดูลของคุณและสร้างไฟล์ชื่อที่modulename_handler_filter_filtername.incนั่น (ดูรหัสด้านบนสำหรับการอ้างอิงโดยนัยถึงไฟล์นี้) อย่าลืมใช้ชื่อโมดูลและตัวกรองที่แท้จริง

  3. วางรหัสต่อไปนี้ลงในmodulename_handler_filter_filtername.incไฟล์นั้น รหัสที่ฉันใช้สำหรับตัวอย่างนี้สร้างชุดของปุ่มตัวเลือกที่แสดงปี ดังนั้นคุณสามารถกรองโหนดตามปีที่สร้างโดยใช้เพียงปีที่โหนดถูกสร้างขึ้น

    class modulename_handler_filter_filtername extends views_handler_filter {
    
      /**
       * Options form subform for setting exposed filter options.
       */
      function value_form(&$form, &$form_state) {
        parent::value_form($form, $form_state);
    
        // get list of years from database
        $query = db_select('node', 'n');
        $query->addExpression("FROM_UNIXTIME(n.created, '%Y')", 'year');
        if (isset($this->view->filter['type'])) {
          $query->condition('n.type', $this->view->filter['type']->value, 'IN');
        }
        $result = $query->orderBy('year', 'ASC')
          ->execute()
          ->fetchAllAssoc('year');
    
        $years = array(
          '0' => t('All'),
        );
        foreach ($result as $k => $v) {
          $years[$k] = $k;
        }
    
        // create form element with options retrieved from database
        $form['value']['year'] = array(
          '#type' => 'radios',
          '#options' => $years,
          '#default_value' => end($years),
        );
      }
    
      /**
       * Alters Views query when filter is used.
       */
      function query() {
        // make sure base table is included in the query
        $this->ensure_my_table();
    
        // retrieve real filter name from view options
        // this requires 'real field' filter option to be set (see code above)
        $real_field_name = $this->real_field;
        // get the value of the submitted filter
        $value = $this->view->exposed_data[$real_field_name];
    
        // finally, alter Views query
        if (is_numeric($value) && $value != 0) {
          /* 
            Having several custom exposed filters, make sure subsitution patterns
            (e.g. :filtername_value below) don't match across different filters.
            I spent some time figuring out why all my filters had the same value.
            It looks like the query skeleton is built first and then all replacements
            are made in bulk. Prefixing value with filter name looks good imo.
          */
          $this->query->add_where_expression($this->options['group'],
            "FROM_UNIXTIME(node.created, '%Y') = :filtername_value",
            array(':filtername_value' => $value));
        }
      }
    }
    

นั่นคือทั้งหมดที่คุณต้องใช้เพื่อให้ตัวกรองแบบกำหนดเองที่ง่ายที่สุดทำงานได้!

โปรดทราบว่าการใช้FROM_UNIXTIMEในเงื่อนไขการSELECTสืบค้นอาจทำให้ฐานข้อมูลของคุณช้าลง


ก่อน: ขอบคุณ! คำแนะนำที่ดีที่สุดตลอดกาล: วินาทีสำหรับผู้ที่ต้องการใช้การสืบค้นขั้นสูง () ดูviews_handler_filter_numeric.inc
hkoosha

$this->query->add_where($this->options['group'], $real_field_name, $this->value['value'], $this->operator);นอกจากนี้ยังมีการใช้มากขึ้นสง่างามที่คุณไม่ต้องเขียนแบบสอบถามและแทนด้วยตนเองเช่น: มันสามารถพบได้ในลิงค์ด้านบน
hkoosha

2
มันใช้งานได้สำหรับฉันใน Drupal 7 อย่างไรก็ตามเพื่อให้งานนี้ฉันต้อง 1) ลบการใช้งานฟังก์ชั่น hook_views_handler และ 2) เพิ่มไฟล์นี้ไปยังไฟล์. info: files [] = inc / modulename_handler_filter_filtername.inc 3) ฉันใช้การเปลี่ยนแปลงทั้งสองนี้ ในโพสต์นี้ 4) ขอบคุณมาก!
Roger

โดยการใช้ไซต์ของเรา หมายความว่าคุณได้อ่านและทำความเข้าใจนโยบายคุกกี้และนโยบายความเป็นส่วนตัวของเราแล้ว
Licensed under cc by-sa 3.0 with attribution required.