4

I can not for the life of me figure out why my cp page will not generate the elements table. On other elementtype pages in the cp there is an XHR request made to a controller called "getElements" and that returns the html for the elements table. On my elementtype page there is no XHR call and the page just sits there with no table.

Here is my template twig:

{% extends "_layouts/elementindex" %}
{% set title = "Pending Account Requests"|t %}
{% set elementType = 'GrnPlatform_AccountRequest' %}

Here is my class for my element

namespace Craft;

class GrnPlatform_AccountRequestElementType extends BaseElementType
{
    public function getName()
    {
        return Craft::t('GRN Account Request');
    }

    public function defineTableAttributes()
    {
        return array(
            'firstName'         => Craft::t('First Name'),
            'lastName'          => Craft::t('Last Name'),
            'email'             => Craft::t('Email Address'),
            'roleRequested'     => Craft::t('Request For'),
            'dateCreated'       => Craft::t('Requested On')
        );
    }

    public function getTableAttributeHtml(BaseElementModel $element, $attribute)
    {
        switch ($attribute)
        {
            case 'dateCreated':
            {
                $date = $element->$attribute;

                if ($date)
                {
                    return $date->localeDate();
                }
                else
                {
                    return '';
                }
            }

            default:
            {
                return parent::getTableAttributeHtml($element, $attribute);
            }
        }
    }

    public function defineCriteriaAttributes()
    {
        return array(
            'userId'            => AttributeType::Number,
            'approved'          => AttributeType::Number,
            'denied'            => AttributeType::Number,
            'roleRequested'     => AttributeType::Mixed,
            'dateCreated'       => AttributeType::Mixed,
            'order'             => array(AttributeType::String, 'default' => 'requests.dateCreated asc')
        );
    }

    public function modifyElementsQuery(DbCommand $query, ElementCriteriaModel $criteria)
    {
        $query
            ->addSelect('users.firstName, users.lastName, users.email, requests.roleRequested, requests.approved, requests.denied, requests.dateCreated')
            ->Join('grnplatform_account_requests requests', 'requests.id = elements.id')
            ->leftJoin('users users', 'users.id = requests.userId');
        if(isset($criteria->userId)) {
            $query->andWhere(DbHelper::parseParam('requests.userId', $criteria->userId, $query->params));
        }
        if(isset($criteria->roleRequested)) {
            $query->andWhere(DbHelper::parseParam('requests.roleRequested', $criteria->roleRequested, $query->params));
        }
        if(isset($criteria->dateCreated)) {
            $query->andWhere(DbHelper::parseParam('requests.dateCreated', $criteria->dateCreated, $query->params));
        }
        if(isset($criteria->approved)) {
            $query->andWhere(DbHelper::parseParam('requests.approved', $criteria->approved, $query->params));
        }
        if(isset($criteria->denied)) {
            $query->andWhere(DbHelper::parseParam('requests.denied', $criteria->denied, $query->params));
        }
    }

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

    public function getEditorHtml(BaseElementModel $element)
    {
        return parent::getEditorHtml($element);
    }
}

Here is my plugin code for the route to the page:

public function registerCpRoutes()
{
    return array(
        'grnplatform'                   => array('action' => 'GrnPlatform/mainIndex'),
        'grnplatform/AccountRequests'   => array('action' => 'GrnPlatform/AccountRequest/mainIndex')
    );
}

Here is the controller that gets routed and renders the template:

namespace Craft;
class GrnPlatform_AccountRequestController extends BaseController
{
    public function actionMainIndex()
    {

        $this->renderTemplate('grnplatform/cp/account_requests');
    }
}

Here is the ElementModel

namespace Craft;

class GrnPlatform_AccountRequestModel extends BaseElementModel
{
    protected $elementType = 'GrnPlatform_AccountRequest';

    public function __toString()
    {
        return ucfirst($this->getAttribute('roleRequested')) . " Request";
    }

    protected function defineAttributes()
    {
        return array_merge(parent::defineAttributes(), array(
            'userId'            => array("type" => AttributeType::Number, "required" => true),
            'roleRequested'     => array("type" => AttributeType::Enum, "values" => array("vendor", "facilitator"), "required" => true),
            'message'           => array("type" => AttributeType::String, 'required' => true),
            'completed'         => AttributeType::Bool,
            'approved'          => AttributeType::Bool,
            'denied'            => AttributeType::Bool
        ));
    }

    public function isEditable()
    {
        return true;
    }

    public function getCpEditUrl()
    {
    }

    public function getFieldLayout()
    {
        return null;
    }
}

Here is the Record for the element:

namespace Craft;

class GrnPlatform_AccountRequestRecord extends BaseRecord
{
    public function getTableName()
    {
        return 'grnplatform_account_requests';
    }

   protected function defineAttributes()
    {
        return array(
            'userId'        => array(AttributeType::Number, 'required' => true),
            'roleRequested' => array(AttributeType::Enum, 'values' => array('vendor','facilitator'), 'required' => true),
            'message'       => array(AttributeType::String, 'column' => ColumnType::Text, 'required' => true),
            'denied'        => AttributeType::Bool,
            'approved'      => AttributeType::Bool
        );
    }

    public function defineRelations()
    {
        return array(
            'element'  => array(static::BELONGS_TO, 'ElementRecord', 'id', 'required' => true, 'onDelete' => static::CASCADE),
            'user' => array(static::BELONGS_TO, 'UserRecord', 'userId', 'onDelete' => static::CASCADE)
        );
    }

    public function defineIndexes()
    {
        return array(
            array('columns'=>array('roleRequested'), 'unique' => false),
            array('columns'=>array('denied'), 'unique' => false),
            array('columns'=>array('approved'), 'unique' => false),
            array('columns'=>array('userId'), 'unique' => false)
        );
    }
}

EDIT 1

I did some manual debugging through the JS on the page and I found this inside the Craft.baseElementIndex:init() function:

// No source, no party.
if ($sources.length == 0)
{
    return;
} 

My code is hitting this because I dont have any sources... Honestly I'm not even sure what my sources would be in this instance...

1 Answer 1

5

You haven't defined any sources for your ElementType.

<?php
public function getSources($context = null)
{
    $sources = [
        '*' => [
            'label' => Craft::t('All entries'),
            'criteria' => [],
            'defaultSort' => ['dateUpdated', 'desc']
        ]
    ];

    return $sources;
}
2
  • Yep! Thanks for the help. Really wish they put a notice in the javascript log, at least when dev mode is on. Would have saved me hours :/ Commented May 11, 2017 at 22:17
  • So annoying to always have one source to be defined. I don't want that sidebar :(
    – Bert Oost
    Commented Sep 20, 2018 at 11:00

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.