1

i have created my first plugin and have hit a bit of a wall... its a plugin with a custom element type, i have it creating its own database table (migrations/install.php) and i have the form displaying, and the bit i am struggling with is the saving bit. I build a new element type based on the form details, then do a Craft::$app->getElements()->saveElement($song); ($song is my element type) , i keep getting a getSection method missing error from my Song element type, but i dont have a sectionId, or need a section for this as it is stored entirely in its own table, what have i not understood about creating and saving a custom element type?

Is there a demo plugin or some example i can learn from?

This is my controller

new SongElementType();
$SongElementType->name = Craft::$app->request->getParam('name', $SongElementType->name);
$SongElementType->band = Craft::$app->request->getParam('band', $SongElementType->band);

then i call save that is in my service from my controller like so:

songscatalogue::getInstance()->SongsService->saveSong($SongElementType)
            $this->trigger(self::EVENT_SONG_SAVED_BEFORE, new SongEvent([
                'song' => $song,
                'isNew' => $isNew,
            ]));

            $success = Craft::$app->getElements()->saveElement($song);

            $this->trigger(self::EVENT_SONG_SAVED_AFTER, new SongEvent([
                'song' => $song,
                'isNew' => $isNew,
            ]));

but i get this error:

Calling unknown method: robsplugin\songscatalogue\elements\Song::getSection()

where am i going wrong?

4
  • 1
    Is the method getSection() inside your Song class static and accessible from where you are calling it? Oct 18 '20 at 23:23
  • I created my element type with a complete set of empty methods for reference, not realising that their presence alone would trigger a different outcome. Once i deleted them all to ones i actually use, it has started working.
    – rob1986__
    Oct 19 '20 at 9:16
  • Just out of curiosity, what made you go for a completely new element type, including your own tables and events, instead of a using normal entries for it? I've been considering something similar for movies with corresponding shows, but I've so far not really found any big advantages to custom element types.
    – KSP
    Oct 19 '20 at 11:38
  • It was purely an exercise to learn more about building plugins. I am finding the documentation to be lacking, with a lot of the docs pointing us to YII's docs, but they dont directly translate into craft.
    – rob1986__
    Oct 19 '20 at 21:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.