I am trying to create a new entry via a form post that will come from a third party, by doing this in my plugin's service class:

$entry = EntryModel::populateModel(array(
    'sectionId' => 1, 
    'typeId' => 2

$entry->getContent()->setAttribute('reference', craft()->request->getPost('someReferenceField'));


The reference field is a custom FieldType (https://github.com/fuel-digital/Craft-Unique-Text-FieldType/blob/develop/uniquetextfield/fieldtypes/UniqueTextField_UniqueTextFieldType.php) where I am validating the submitted value is unique for this EntryType (in this case an Entry rather than a User/Asset/etc).

That FieldType works fine in the control panel but I'm getting an error when submitting via my plugin as $this->element is null within the FieldType's valueExists() method.

I can get around this by creating a new EntryModel like so:

 * Checks if another entry exists with the given value
 * @param  string $value The value to check
 * @return bool
private function valueExists($value)
    if ( ! $this->element)
        $this->element = new EntryModel();

    $existing = craft()->elements->getCriteria($this->element->elementType)->first(array(
        $this->model->handle => $value

    return ($existing && $existing->id != $this->element->id);

But that means the FieldType will only work with Entry elements, and not Users, Assets etc.

As the plugin works fine in the control panel, I assume I'm missing something in my plugin service?

1 Answer 1


Found a solution by how Entry Form handles it.

Assuming you've posted fields with names such as jobData[reference]:

$fieldsLocation = craft()->request->getParam('fieldsLocation', 'jobData');


The setContentFromPost() method of the EntriesController class sets the ElementType.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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