1

I'm trying to create my own custom field type, however, when I go to load the view, the var's within my display_field function, are Undefined. All I'm trying to do, is Load a view file, then in the view, loop through and array.

Example:

function display_field($data)
    {
        $entry_id = $this->EE->input->get_post('entry_id');
        $query = $this->EE->db->query("SELECT * FROM exp_channel_images WHERE entry_id = '$entry_id'");

        $channelImages = array();

        if ($query->num_rows() > 0)
        {
            foreach($query->result_array() as $row)
            {
               array_push($channelImages, $row);
            }
        }

        return $this->EE->load->view('index', $channelImages, TRUE);
    }

Then, my view file is called index.php, and is located under:

/third_party/channel_images_sources/views/index.php

1

First, for new entries the get_post(entry_id) probably isn't set (check and verify it before running your query). Second, the view loader extracts the vars array passed to it, and your array doesn't have keys. instead of passing $channelImages array, try passing it inside an array, something like array('chanimg' => $chanimg)

See example below, and the part "adding dynamic data to the view"
http://ellislab.com/codeigniter/user-guide/general/views.html

$data = array(
  'chanimg' => array()
);

$entry_id = $this->EE->input->get_post('entry_id');
if ($entry_id > 0){
    // query
    // $data['chanimg'][] = $row;
}

return $this->EE->load->view('index', $data, TRUE);
| improve this answer | |
  • Thank you, that was it. Now I simply reference "$chainimg" in the view, and my data is there! – Whnunlife Apr 10 '13 at 14:45
1

The display_field function of field type will not load view file so you need to return the data / content which you would like to show at edit or publish forms and SAEF.

Like:

function display_field($data){
/***
other code
**/

return "<div>SOME DATA or VARIABLES</div>";

}
| improve this answer | |
  • While true that you need to return data/content from the display_field method for the publish screen. It is possible to use a custom view (say a whole table or a map with javascript) and fetch that view with the second parameter of load view set to true (return as string) and pass that data along from display field. – GDmac Apr 15 '13 at 21:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.