Questions tagged [plugin-development]

Questions having to do with constructing plugins.

Filter by
Sorted by
Tagged with
15
votes
1answer
3k views

What is getContent() / the ContentModel, and how do I use it to get and set custom field values?

If I want to retrieve content on an element, should I be calling $entry->$fieldHandle or $entry->getContent()->$fieldHandle? What’s the difference? Likewise, if I’m setting new content on an ...
14
votes
2answers
4k views

How to save Matrix data programmatically in Craft 3

How can you programmatically save an entry that has a Matrix field in Craft 3? (Something like this gist for Craft 2)
7
votes
3answers
2k views

Craft Commerce: Validation on Address Fields

Can someone break down how I should validate empty address fields as a plugin for Craft Commerce and take the following answer further? The Craft Commerce documentation provides the following example ...
7
votes
1answer
1k views

Is it possible to use the ElementType Modal UI for a custom Field Type Plugin?

So I'm writing a plugin to try and create a relationship between two entry sections that is a two way street (Many to Many). I would love to co-opt the UI and functionality of the ElementType modal ...
23
votes
1answer
2k views

How can my plugin generate a generic modal window in the Control Panel?

I'm working on a plugin, and it requires a modal to be displayed when the user interacts with a field of my Field Type. When the user finishes interacting with the modal, they would click "Done". ...
10
votes
5answers
6k views

What's the best way to skip CSRF for plugin controllers?

I'm working on a plugin that'll create a private API for content authors to post entries to Craft from desktop-based scripts. These scripts post data to plugin controller actions (anonymous-allowed), ...
5
votes
2answers
2k views

How to save a matrix content of a new entry in my plugin?

I have a section of type channel with a matrix field. I want to know how I would save the matrix content while I save the entry as well. I know I can save the entry as follows: $entry = new ...
4
votes
2answers
2k views

Save contact form data to database

I have your standard contact form. (Name, email etc.) When the user clicks "save" I want to capture what was entered via a plugin & have a CP page to show the people who have filled out the form. ...
3
votes
1answer
994 views

Search Contact Person Plugin via Ajax

I'm new to Craft and I'm trying to achieve the following with a Craft Plugin A user visits my site and can input a zip code to receive the corresponding contact person's name, phonenumber and image. ...
8
votes
2answers
2k views

How does a plugin include it's own config options?

When building a plugin, how does one define user editable config files? Would it be better to create a settings UI in the plugin and have the user edit those variables in the CP? Or is there a way to ...
7
votes
1answer
1k views

How can I do more complicated queries with an ElementCriteriaModel?

I need to join my 3rd party table with a standard ElementCriteriaModel (ECM). I'd like to start with an ordinary ECM, so my plugin will be compatible with any element type which may exist, now or in ...
7
votes
2answers
3k views

How to add a custom button at the right section of an entry?

I would like to add a custom button in the right section of a channels entry form in the CMS. The this button needs to go to a custom url of the plugin that I have created. The link should contain ...
2
votes
1answer
690 views

Is it possible to define optional route segments?

Is it possible to have optional route segments/variables with getCpRoutes() and getSiteRoutes()? I'd like the following route to work, even if the URI only contains one segment ("myplugin") rather ...
13
votes
2answers
2k views

How do we create a pre-defined Asset upload field in a plugin that integrates with the native Craft Assets file manager?

We've got a plugin. It doesn't have the ability to add custom field types. We're pre-defining all of the fields. One of the fields needs to be an Asset field. A user should be able to upload an ...
10
votes
1answer
3k views

How to use plugin settings

I am learning craft and trying add settings page for my plugin. I read that need to define settings like, class CocktailRecipesPlugin extends BasePlugin { // ... protected function ...
2
votes
1answer
2k views

Need help with plugin for SAML authentication

I'm trying to put together a plugin that allows for SAML authentication with Craft. We're working on a site that will be locked down but employees will be able to authenticate to the site using SAML. ...
9
votes
2answers
1k views

Plugin Dev - Load third party library

What is the preferred way of loading a third party library in a plugin? I'm trying to develop a plugin to extend SproutForms so that users get signed up to Campaign Monitor. So far I've got an ...
7
votes
3answers
2k views

Getting all content, including Matrix fields from an EntryModel

Initially I was doing $entry->getContent()->getAttributes(), but noticed my Matrix fields were all null. What is the right way to do this? I see that $entry->myMatrixField gives me the ...
6
votes
4answers
8k views

Prescribed way to set cookies (from plugin or Twig)

From either a plugin or a Twig template, is there a prescribed way to set (and get) cookies? I've seen https://github.com/lewisjenkins/craft-lj-cookies/, which just uses PHP's setcookie. Also ...
5
votes
2answers
751 views

Categories field is losing its value when programmatically updating other fields’ values

I have a webhook that fetches an entry, modifies one of its custom Text field values and then saves the entry. However there is also a required Categories field on the entry, and when I do this, I get ...
5
votes
1answer
1k views

How do I get elements using getCriteria from a specific date?

I'm trying to get elements in a section created on a specific date. How do I do that? This is what I've been trying with: $criteria = craft()->elements->getCriteria(ElementType::...
4
votes
2answers
468 views

What's the difference between ElementCriteriaModel and DbCommand?

Forgive me if this is a stupid or naive question... I generally think of an ElementCriteriaModel as being a "search object". Specifically, it's a search object which revolves around a specific type ...
1
vote
1answer
801 views

Change asset name on upload

public function init() { craft()->on('assets.onBeforeSaveAsset', function(Event $event) { $asset = $event->params['asset']; $asset->setAttribute('filename', 'new name'); ...
8
votes
1answer
501 views

Console script tags garbling up non-html output (e.g. json) when in devMode

I have a plugin returning json from a controller action using $this->returnJson(...). Works great in production, but when devMode = true, the json is invalidated because Craft is still dumping the ...
6
votes
2answers
1k views

How do I save a Plugin's date settings-field?

I've got a plugin with some custom settings, like this: protected function defineSettings() { return array( 'serviceendpoint' => array(AttributeType::String, 'required'=>...
6
votes
1answer
1k views

How can I add custom validation rules to my plugin settings?

I have a plugin that needs to perform some custom validation on one of its settings. How would I go about doing that?
6
votes
2answers
328 views

Delete / change Status from CP via Ajax (trigger event)

In a plugin init function I'm listening to entries.saveEntry and entries.deleteEntry. This works perfectly if I'm editing entries from their detail view. If I 'bulk' change or delete entries, these ...
4
votes
1answer
696 views

Use `onBeforeSaveEntry` event to update EntryModel properties?

With my plugin I listen for onBeforeSaveEntry events and make changes to the $entry EntryModel. It works and I can change most properties from this function, but I have problems updating sectionId, ...
8
votes
1answer
672 views

Is it possible to hook into image transforms?

I have a somewhat unique thumbnail need for a certain site. I need to watermark images with custom text (per image, not globally across the site). Is there a way to hook into the Craft image transform ...
4
votes
1answer
530 views

Does Twigs 'addGlobal' exist in Craft?

I know Craft gives us access to Twigs getTwig template service; so when I attempt to use the addGlobal function like so: craft()->templates->getTwig()->addGlobal('var', $var); ... nothing ...
10
votes
1answer
350 views

Best way to implement my own statuses on my custom element type?

I'm working on a plugin with its own element type, I'm trying to add custom statuses to the element type. What is the best way to proceed? I have hasStatuses set to true and getStatuses is where I ...
10
votes
2answers
995 views

Update Asset Index in Batches

I have 80,000+ files on S3 that need to be added to Craft. The Update Asset Indexes works, but takes over 24 hours and cannot be interrupted. Does anybody have a way to programmatically index assets ...
7
votes
1answer
1k views

How do I add a block to a matrix that already has content via a plugin?

I'm building a plugin that adds blocks to a matrix in an entry from the front end. I got it up and running by following the answer in this post: How to save a matrix content of a new entry in my ...
7
votes
1answer
835 views

Updating an entry after an event action within onSaveEntry

I have written a plugin that checks if an entry has been saved and sends the user an email from within an onSaveEntry event. This works fine. But I now need to also update this entry to flag that an ...
5
votes
1answer
1k views

Is it possible to add a custom field to User model from a plugin?

Within a plugin I'm working on, I'll be adding user synchronisation with a third-party database to enable SSO, so will be creating users within Craft based on an interaction from this other service. ...
3
votes
3answers
2k views

Customize activation and other system emails

Just as the title indicates, I'm looking for a way to customize the email template of the "click here to activate your account" email that is sent to users when they register. I would also like to ...
9
votes
2answers
509 views

How to integrate custom statuses in a workflow style custom element type?

I made a lot of progress on my custom element type from this post and with the help of @selvinortiz: Best way to implement my own statuses on my custom element type? I know have custom statuses that ...
9
votes
1answer
542 views

How can I best extend TagModel to add a `count` property?

I'm attempting to write a plugin to extend tags by adding a count property that'd be useful identifying popular tags, making a tag cloud, and anything else where a count would be useful. The Goal ...
7
votes
2answers
3k views

How do I reference the settings of my plugin?

In the main Plugin.php file of my plugin, I can retrieve the plugin's settings like this: $this->getSettings() How do I retrieve those same settings from elsewhere in the plugin? I want to ...
6
votes
2answers
3k views

Is it possible to access Twig variables in a plugin variable using PHP?

I have a controller that returns route variables to the page: craft()->urlManager->setRouteVariables(array( 'errors' => $entry->getErrors() )); On the page that gets loaded, I can ...
5
votes
4answers
1k views

Memory issue when updating all entries from a channel (allowed memory size of X bytes exhausted (tried to allocate Y bytes))

I'm trying to do a stock update for all entries within a channel with a cronjob once a day, and I can't achieve it, because I always get Allowed memory size of X bytes exhausted (tried to allocate Y ...
5
votes
2answers
1k views

How to retrieve entries from two entry-types ordered by different date-fields?

I have two entry-types, 'Events' and 'Reports'. 'Events' have a matrix-field ('eventMultiDate') that allows authors to assign multiple dates to an event (recurring). 'Reports' have the default '...
3
votes
1answer
670 views

How can I get the requested entry from a plugin?

If I’m accessing an entry’s URL (the 4th check in Routing), how can I get the entry that the URL matched from a plugin?
15
votes
3answers
4k views

How to get absolute path to asset file?

I have some csv files uploaded as assets and now I need to fopen() them from my plugin. I have an AssetFileModel but I have not been able to work out how to construct a full path to the file. I can ...
9
votes
1answer
2k views

How to iterate over matrix field from a plugin

I'm writing a plugin where I have the id of an entry, that entry has a matrix field, and I want to iterate over the blocks of the matrix field. In php. How do I do that? Pointers to good documentation ...
5
votes
2answers
1k views

Best way to include attachments using EmailService [Craft Plugin]?

I've got Email Service working correctly (http://buildwithcraft.com/docs/apis/emailservice) but what's the best way to include attachments when using this service? Here's what I've got so far: $...
5
votes
1answer
547 views

Get the type of action and the elements(s) that an action is being performed on when listening to elements.onPerformAction?

I have a plugin that needs to do something when an entry is deleted. I've recently discovered that the entries.deleteEntry event is not fired when deleting bulk entries (the list view with checkboxes) ...
3
votes
2answers
185 views

Fire event first time future dated entry goes 'live' from 'pending'

Is there a way I can run some code from a plugin when a future dated entry goes 'live' from 'pending'? I am trying to send an email update that a new entry has been posted. The related post below ...
8
votes
1answer
3k views

Documentation on creating tasks

I'm looking to integrate tasks into a plugin I'm building. Whilst I know that the Craft documentation shows the Tasks Service/Model properties/methods in the full class reference, I can't find ...
6
votes
1answer
873 views

Get related entry data within a plugin (ElementCriteriaModel) without extra queries

I have the following code: $criteria = craft()->elements->getCriteria(ElementType::Entry); $criteria->section = 'submissions'; // Loop foreach ($criteria as $entry) { echo $entry->id....

1
2 3 4 5