2

I'm attempting to finish my first fieldtype plugin (how exciting!) but having an issue with displaying options in a select dropdown in an entry.

My ColumnWidth_WidthsFieldType has this:

<?php

namespace Craft;

/**
 * Widths Fieldtype
 *
 * Allows entries to select associated widths
 */
class ColumnWidth_WidthsFieldType extends BaseFieldType
{
    /**
     * Get the name of this fieldtype
     */
    public function getName()
    {
        return Craft::t('Column Widths');
    }

    /**
     * Get this fieldtype's column type.
     *
     * @return mixed
     */
    public function defineContentAttribute()
    {
        // "Mixed" represents a "text" column type, which can be used to store arrays etc.
        return AttributeType::Mixed;
    }

    /**
     * Get this fieldtype's form HTML
     *
     * @param  string $name
     * @param  mixed  $value
     * @return string
     */
    public function getInputHtml($name, $value)
    {
        // call our service layer to get a current list of widths
        $widths = craft()->columnWidth_widths->getAllWidths();

        // create an array for the widths 
        $widthsOptions = array();

        // populate the $widthsOptions array with $widths
        foreach ($widthsOptions as $widths )
        {
            $widthsOptions[$widths->id] =  $widths->name;
        }

        return craft()->templates->render('columnwidth/_fieldtypes/widths', array(
            'name'      => $name,
            'values'    => $value,
        ));
    }
}

My columnwidth/_fieldtypes/widths has this:

{% import "_includes/forms/" as forms %}
​
{% set widths = craft.columnwidth.getAllWidths %}

{{ forms.selectField({
    instructions: "Select the width of your column"|t,
    id: 'columnWidth',
    name: 'columnWidth',
    options: widths
}) }}

But I'm getting this error:

Craft\ColumnWidth_WidthsModel and its behaviors do not have a method or closure named "optgroup".

The only post I found was here:

Displaying a list of fields in a form select

I'm sure I'm missing something and any help is appreciated!

2

Few things - you need to add the options to the render function so they are passed into the template. Also, check what I did below for the $widthsOptions. Each option needs a label and value key/value pair.

public function getInputHtml($name, $value)
{
    // call our service layer to get a current list of widths
    $widths = craft()->columnWidth_widths->getAllWidths();

    // create an array for the widths 
    $widthsOptions = array();

    // populate the $widthsOptions array with $widths
    foreach ($widths as $width )
    {
        $widthsOptions[] = array(
            'label' => $width->name, //this should be whatever you want to display in dropdown
            'value' => $width->name //this should be the value you want passed to the database
        );
    }

    return craft()->templates->render('columnwidth/_fieldtypes/widths', array(
        'name'      => $name,
        'values'    => $value,
        'options'   => $widthsOptions
    ));
}

Your field type template should be something like this:

{% import "_includes/forms/" as forms %}
​
{{ forms.selectField({
    instructions: "Select the width of your column"|t,
    id: 'columnWidth',
    name: 'columnWidth',
    options: options,
    value: values
}) }}
| improve this answer | |
  • what would cause the selected value to not save? on save the value keeps reverting back to the first value. – Mark Busnelli Jr Oct 8 '15 at 13:59
  • Is the value saving correctly in the database and just not showing up as the selected item, or is it not saving at all? – Aaron Berkowitz Oct 8 '15 at 14:02
  • It's saving, so at least there's that :) – Mark Busnelli Jr Oct 8 '15 at 14:06
  • 1
    Just updated the answer - you need to have value: values in the selectField macro options so it knows which value is currently saved in the database. – Aaron Berkowitz Oct 8 '15 at 14:09
  • Nice, really wish there was some documentation on this besides rummaging around in the forms :) – Mark Busnelli Jr Oct 8 '15 at 14:10

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.