2

I have a plugin with a FieldType - I used the CocktailRecipes plugin as a template.

It extends BaseFieldType; it has 3 methods (getName, defineContentAttribute [AttributeType::Mixed] and getInputHtml)

It it working fine on a local PHP install.

However, on the server, when I create a new field, the plugin's custom field type isn't listed in the drop-down; this works fine on my local PHP install.

If I use my local install connected to the server's database, I can add the field type - but if I look at the field list in the admin back on the server, the field Type isn't the string returned but the getName function in the FieldType model (as it usually is) - it's the full class name, highlighted in red (it has a wrapping element with a class of "error").

I can't see anything in the log files about why - even in dev mode.

The only differences I can think of between my local and the server setups is that the PHP version is higher on the server - and the server OS is case-sensitive for file names.

I've checked the casing used, and it looks correct based on the CocktailRecipes plugin example.

It feels like the issue is that the class can't be found on the server, despite it being there; and perhaps a casing issue, which is being masked on my local machine, is the reason the issue isn't occurring locally.

Has anyone encountered this issue before? With no error messages being logged, I haven't had much luck digging out the issue.

What I'm basing it on:

<?php

namespace Craft;

/**
 * Ingredients Fieldtype
 *
 * Allows entries to select associated ingredients
 */
class CocktailRecipes_IngredientsFieldType extends BaseFieldType
{
    /**
     * Get the name of this fieldtype
     */
    public function getName()
    {
        return Craft::t('Cocktail Ingredients');
    }

    /**
     * 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 ingredients
        $ingredients = craft()->cocktailRecipes->getAllIngredients();

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

What works fine locally:

<?php
namespace Craft;

class ECOffers_OfferPeriodFieldType extends BaseFieldType {

    public function getName() {
        return Craft::t('Offer period');
    }

    public function defineContentAttribute(){
        return AttributeType::Mixed;
    }

    public function getInputHtml($name, $value) {
        $offer_periods = craft()->eCOffers_offerPeriods->getOfferPeriodsForLocale($this->element->getContent()->getAttributes()['locale']);
        $options = array();
        if ($offer_periods) {
            foreach ($offer_periods as $offer_period) {
                $options[] = array(
                    'label' => $offer_period->name.': '.$offer_period->fromDate->format('H:i D jS M Y').' - '.$offer_period->toDate->format('H:i D jS M Y'),
                    'value' => $offer_period->id
                );
            }
        }
        return craft()->templates->render('ecoffers/_fieldtypes/offerperiods', array(
            'options' => $options,
            'value' => ((!is_null($value)) ? $value:craft()->eCOffers_offerPeriods->getCurrentOfferPeriod($this->element->getContent()->getAttributes()['locale'])->id)
        ));
    }
}

Local instance with remote database

Server instance with same database

1

I think you're on the right track with the case-sensitivity check.

Make sure that not only the class names match up case-wise, but the file names on the file system match case-wise as well.

| improve this answer | |
  • The directory was "fieldTypes" instead of "fieldtypes" - worked on Mac OS X, failed on CentOS. The lesson here being that if your plugin's custom field types aren't listed in the edit field drop-down, or the Type is listed in red with the full class name (instead of whatever getName is set to return) for existing field(s), then it means Craft can't find the class file. Still would of liked to of had this logged somewhere in Craft! – Tom Dec 12 '14 at 9:29

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.