4

I'm creating a plugin with its own ElementType, i've been using the Events plugin by P&T for reference, so I thought everything was going fine.

Now when I go to my elements index, I seem to just get a listing for all elements, regardless of anything. What could cause this? I have my element type in my template like

{% set elementType = 'Custom_ElementType' %}

So if I change this to

{% set elementType = 'Entry' %}

I get a list of all entries with the correct sections and whatnot, which is what I expect, but when I change it to the former I get this:

enter image description here

I've obviously overlooked something but I can't figure it out, if anyone else has overcome something similar i'd be grateful for any pointers.

Update

Here is the code i'm using:

Plugin_FooElementType.php

<?php
namespace Craft;

class Plugin_FooElementType extends BaseElementType
{
  public function getName()
  {
    return Craft::t('Foo Element');
  }
  public function getSources($context = null)
  {
    $sources = array(
      '*' => array(
       'label' => Craft::t('All sources')
      )
    );
    return $sources;
  }

  public function populateElementModel($row)
  {
    return Plugin_FooModel::populateModel($row);
  }

  public function defineTableAttributes($source = null)
  {
    return array(
     'id' => Craft::t('ID'),
     'handle' => Craft::t('Handle'),
     'path' => Craft::t('Path'),
    );
  }
}

_index.html

{% extends "_layouts/elementindex" %}
{% set title = "Endpoints"|t %}

{% set elementType = 'Plugin_Foo' %}

{% set tabs = {
  events:    { label: "Events"|t, url: url('events') },
  requets: { label: "Requests"|t, url: url('plugin/foos') }
} %}

Plugin_FooModel.php

<?php
namespace Craft;

class Plugin_FooModel extends BaseElementModel
{
 protected $elementType = 'Plugin_Foo';


 protected function defineAttributes()
 {
    return array_merge(parent::defineAttributes(), array(
        'requestId' => AttributeType::Number,
        'handle' => AttributeType::Handle,
        'path' => AttributeType::String,
    ));
 }

 public function getCpEditUrl()
 {
    $relation = $this->getRelation();
    if ($relation)
    {
        return UrlHelper::getCpUrl('plugin/'.$relation->handle.'/'.$this->id);
    }
 }


 public function getRelation()
 {
    if ($this->relatedId)
    {
        return craft()->plugin->getRelationById($this->relatedId);
    }
 }
}
2
+50

It is likely just a typo in your template. Can you ensure that if your element type class name is of the format Custom_PluginElementType that your template code is then in the following format:

{% set elementType = 'Custom_Plugin' %}

Or if your element type class name is of the format CustomElementType that your template code is then in the following format:

{% set elementType = 'Custom' %}
| improve this answer | |
  • Yeah as far as I can tell everything looks as though its correct, I have PluginHandle_FooElementType as the classname format which is referenced correctly in the templates (ended up copy and pasting it for sanity) and no dice. if I change {% set elementType = 'Custom_Plugin' %} to something random the admin page throws a 404, which makes me think it's referenced correctly. – Alec Ritson Dec 14 '15 at 8:08
  • so is your template code then {% set elementType = 'PluginHandle_Foo' %}? – Ben Croker Dec 14 '15 at 9:31
  • Yes, it is. I've updated my question to give more details on what i've got set up :) – Alec Ritson Dec 14 '15 at 9:39
  • can you post your Plugin_FooModel code too? – Ben Croker Dec 14 '15 at 9:49
  • Sure, sorry should have done that before! – Alec Ritson Dec 14 '15 at 9:54
1

I just had the same issue. You forgot to add the modifyElementsQuery method to your Plugin_FooElementType class:

public function modifyElementsQuery(DbCommand $query, ElementCriteriaModel $criteria)
{
    $query->join('plugin_foos plugin_foo', 'plugin_foo.id = elements.id');
}
| improve this answer | |

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.