9
votes
Accepted
Get array of titles instead of whole collection of entries' attributes
Unfortunately, the answer differs pretty dramatically for native properties with "stable" column names and for Content, which (as of Craft 3.7.0) now contain a "suffix" that (in ...
6
votes
Get array of titles instead of whole collection of entries' attributes
Just to expand on the caveat regarding field suffixes in Craft 3.7+, I asked on Github what the recommended way to include field suffixes in a custom query is. Here's brandonkelly's response:
The ...
5
votes
How to override homepage content through plugin?
You can inject HTML to the end of the body using View Events:
use craft\web\View;
use yii\base\Event;
Event::on(
View::class,
View::EVENT_END_BODY,
function() { echo 'Foobar'; }
);
The ...
5
votes
Accepted
How to find next sibling from entry order of parent entries field
nextSiblingOf() and related methods will only work with Structure entries. What you need instead is next() / prev(): https://docs.craftcms.com/api/v3/craft-base-element.html#next
(At the time of ...
5
votes
"Template not found" error when attempting to access static web page
The problem was specific to my local set up. The dockerfile for this project did not mount the entire /web/ folder, so the live application was ignoring local changes I made to it.
Solution:
Change ...
4
votes
Accepted
Load More Entries Button
The Sprig plugin will save you a lot of trouble, and dynamically loading more entries is a perfect use-case for it. If you prefer to use jQuery, then by all means do so, but keep in mind that it is ...
4
votes
Accepted
Can't register custom adjuster with Craft CMS commerce
The error indicates that you're type-hinting the wrong class. Note this line in the error message:
Argument 1 passed to amss\amss\AMSS::amss\amss{closure}() must be an instance of amss\amss\...
4
votes
Accepted
How to create a follow system "button" for "Event" entries?
This functionality is comprised of multiple components – you need to build the interface, keep track of which visitor follows which events, potentially do something with that information and change up ...
4
votes
How to make custom element types restorable?
You have 2 solutions:
Implement your own Restore action, or
Be lazy and reuse \craft\elements\actions
I won't cover implementing you own action, let's be lazy!
Declare the action
To do this, you ...
4
votes
Accepted
Trigger Webhook when Updates are Available
As far as I can tell by looking at the source code and searching the Events reference, Craft doesn't dispatch any relevant events that could be used to make the Webhooks plugin trigger a POST request ...
3
votes
"Unknown package has no name defined" composer error on upgrade
rm vendor/composer/installed.json
composer install
Maybe installed.json was generated with composer 2.x and you are trying run it now with composer 1.6 or 1.8.
Also maybe update composer it also ...
3
votes
How do you save a new Entry to be enabled on 2+ Sites?
Since 3.4, you do this by passing an array of site ID/site-enabled mappings like so:
$entry->enabledForSite = [1 => true, 2 => true];
Where 1 and 2 are your chosen site ids.
3
votes
Send automated emails to users that haven't logged in for X amount of time
Not sure if there is a plugin that does this automatically, but you can build a small module that accomplishes this. Since it should run automatically in the background, I would create the ...
3
votes
Generating srcset in a similar way to WordPress
You can write a function, a utility template or a macro to make repetitive tasks like generating responsive images easier. I usually prefer templates, because they're easy to extend and they can take ...
3
votes
Accepted
Output post category
At the moment, you are loading all categories in the line {% set categories = categoryEntry.all() %} and that's why you are getting them all for each entries.
What you need to do is to fetch your ...
3
votes
Accepted
Eager loading a photo within a matrix field
You've eager-loaded the matrix block(s), but not the image(s) within them. Change to something like this to eager load both, assuming you have named the block type "asset" and the asset ...
3
votes
How to reliably determine if an entry is being duplicated?
If you don't mind me rearranging your code a bit, I might go with something more like this...
Event::on(Entry::class, Entry::EVENT_BEFORE_SAVE, static function (ModelEvent $event) {
/** @var ...
3
votes
Accepted
How does Craft CMS gets data from a 3rd part API?
Unless there's a community plugin specifically built for connecting to this API, you're gonna have to build your own. But a simple module to connect to a third-party API and get some basic data is not ...
3
votes
Accepted
How to set SMTP Enrcyption with an environment variable
You can name the environment variable whatever you want. The trick is that the variable's value needs to be either none, ssl or tls (i.e. the three options that are supported for the "Encryption ...
3
votes
Accepted
Identify event origin (e.g. via admin vs module code)?
Discovered here you can use Craft::$app->getRequest()->getIsCpRequest() to check if it was a request via control panel.
3
votes
Accepted
The craft way of creating fields through code?
Craft gives you the 'best of both worlds' in this regard with the project config. You can just create fields, sections and entry types through the interface. Every change you make is automatically ...
3
votes
Accepted
Display time along with date in admin listing page
The output format for DateTime attributes is hard-coded in the craft\base\Element class, so there's no configuration you can use to change it. See the source code here for details.
You can, however, ...
3
votes
Accepted
Feed-me import nested categories from CSV/Google sheets
This is how I did it: https://craftquest.io/lessons/populating-nested-categories-from-a-google-sheet (free video)
I structured the spreadsheet so it has a flat list of all of the categories in the ...
3
votes
How do you deal with random class and cache?
Randomness and caching are always gonna be at odds. Before you jump at complicated solutions, I'd have a talk about precise requirements, including mentioning the trade-offs involved. Do you really ...
3
votes
Accepted
How do you deal with random class and cache?
One workaround to problems like this is using string placeholders.
The basic idea is that instead of rendering the actual dynamic value inside {% cache %} tags, you can output a static string ...
3
votes
Accepted
Template not found: admin
CP_PANEL environment variable needs to be set to the same as your current Valet URL
3
votes
Accepted
How to increase control panel pagination item count?
This has been asked before quite recently, and I don't believe there's been a better solution posted since then. Check out the answer on that post to see the solution, which involves using a bit of ...
3
votes
Accepted
Craft Commerce: Multiple redirect submit buttons from cart form (checkout/update cart) without ajax
You may be able to use the browser-native <button type="submit"> feature!
Check this out:
<form method="POST">
{{ csrfInput() }}
{{ actionInput('commerce/carts/...
3
votes
Accepted
.all() — Looping through element queries directly has been deprecated
The issue is likely this line:
{% set itemImages = itemImages|merge(itemImage.relatedImages) %}
The |merge filter treats the relatedImages field as an array, which is why you get that deprecation ...
3
votes
Accepted
GraphQL IDE doesn't show up in Craft Admin
GraphQL support in Craft requires a Craft Pro license.
https://craftcms.com/pricing
Do you have one for that project? If you're just working locally, you can enable a trial.
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
craft3 × 1829plugin-development × 232
templating × 123
plugins × 74
php × 73
plugin-craftcommerce2 × 71
assets × 67
plugin-craftcommerce × 67
multi-site × 66
matrix × 60
entries × 59
modules × 53
localization × 50
database × 50
users × 49
categories × 45
plugin-elementapi × 39
control-panel × 35
search × 35
composer × 35
redactor × 30
error × 30
field × 29
emails × 27
admin × 27