1

I have a custom element type with 2 custom fields, 'name' and 'email'. I can't seem to get the index search working for this element type. I defined defineSearchableAttributes and returned an array with these 2 attributes. Searching on the title field works though.

<?php
namespace Craft;

class Newsletter_SubscriptionElementType extends BaseElementType {

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

public function hasContent() {
    return true;
}

public function hasTitles() {
    return true;
}

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

public function defineCriteriaAttributes() {
    return array(
        'name'      => AttributeType::String,
        'email'     => AttributeType::Email
    );
}

public function defineSearchableAttributes() {
    return array('name','email');
}

public function defineTableAttributes() {
    $attributes = array(
        'title'         => Craft::t('Title'),
        'name'          => Craft::t('Name'),
        'email'         => Craft::t('Email'),
        'dateCreated'   => Craft::t('Date Created')
    );

    return $attributes;
}

public function getTableAttributeHtml(BaseElementModel $element, $attribute) {
    return parent::getTableAttributeHtml($element, $attribute);
}

public function getSources($context = null) {
    $sources = array(
        '*' => array(
            'label'    => Craft::t('All subscriptions'),
        )
    );

    return $sources;
}

public function modifyElementsQuery(DbCommand $query, ElementCriteriaModel $criteria) {
    $query 
        ->addSelect('newsletter_subscriptions.name, newsletter_subscriptions.email')
        ->join('newsletter_subscriptions newsletter_subscriptions', 'newsletter_subscriptions.id = elements.id');
}
}
  • That method only comes into play when you're saving your custom element... is that how you're testing? – Brad Bell Jun 12 '17 at 23:30
  • You mean that a new entry has to be posted after I defined that method before craft can search that entry? – Thomas Vansteelandt Jun 13 '17 at 7:52
  • Correct... Craft will extract the search keywords for an element after it is saved. If you've added that method after you have existing elements, they'll need to be resaved. – Brad Bell Jun 13 '17 at 21:08
2

Craft will only extract the search keywords for an element during the element saving process.

If you have existing custom elements already saved, then you go in and add the defineSearchableAttributes() to your element type, you'll need to re-save the existing elements in order for their search keywords to be indexed.

Or now that I think about it... the easier way would probably be to just run the "Update Search Indexes" tool in the Control Panel under Settings->Tools. It will do the same thing for you.

|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.