Skip to main content
Share Your Experience: Take the 2024 Developer Survey
24 votes
Accepted

How to save Matrix data programmatically in Craft 3

Saving Matrix fields programmatically got a lot easier in Craft 3 thanks to all the content refactoring. use craft\elements\Entry; use craft\elements\MatrixBlock; // Figure out the section & ...
Brandon Kelly's user avatar
22 votes
Accepted

New Project: Wait for Craft 3 or start now with Craft 2?

My recommendation would be to go with Craft 2: A bird in the hand is worth two in the bush There is no doubt that Pixel & Tonic has turned their focus to Craft 3, but software projects are ...
Ben Parizek's user avatar
  • 13.5k
20 votes
Accepted

{parent.uri}/{slug}

To pull off the same functionality in Craft 3, you could use something like this: {entry.last().uri}/{parent.uri}/{slug}
Brad Bell's user avatar
  • 67.5k
17 votes
Accepted

How to handle the SECURITY_KEY?

First a bit of history: Craft 2 has a similar concept where it would store a unique random security key in craft/storage/runtime/validation.key. Alternatively, you could set the validationKey config ...
Brad Bell's user avatar
  • 67.5k
17 votes

Invalid plugin: No plugin exists with the handle - Craft 3

For future reference, I had the same error and resolved it with Craft Support. In my case the project.yaml file got out of sync with what was in the database. Running craft project-config/rebuild ...
mjr's user avatar
  • 1,405
17 votes

"Unknown package has no name defined" composer error on upgrade

Very similar to Devin's answer. I came up against this on Friday and I had to follow these steps to resolve this issue. Nuke the vendor folder within the project. rm -rf vendor/ Nuke the composer....
Terry Upton's user avatar
  • 1,894
16 votes

Craft CLI RC1 Connectivity Issues with MAMP Pro

I gave everything a try, but the only thing that worked for me was ensuring "Allow network access to MySQL" was ticked in MAMP. Screenshot: http://c42.co/Ur30EJ
Andrew Hahn's user avatar
15 votes
Accepted

Craft 3 language switcher

The concept of localization has changed in Craft 3, there are not only locales anymore - now you have different sites and site groups. Example without multisite - only different locals You can get ...
Robin Schambach's user avatar
14 votes
Accepted

Craft CLI RC1 Connectivity Issues with MAMP Pro

After playing with this a little further it seems the unixSocket setting is necessary to get this working so I added: 'unixSocket' => getenv('DB_SOCKET') To config/db.php and then added: ...
Tim Knight's user avatar
  • 1,022
14 votes

Plugin logging in Craft 3

Another perhaps simpler approach is to write to your own log file without using the Craft logger at all. $file = Craft::getAlias('@storage/logs/pluginhandle.log'); $log = date('Y-m-d H:i:s').' '.$...
Ben Croker's user avatar
  • 7,526
13 votes

How do I upload an asset in Craft 3, via PHP

I finally figured out how to upload an asset, however I still don't understand why the insertFileByLocalPath has been removed. /** * @param UploadedFile $uploadedFile * @param int $folderId * @...
SteveEdson's user avatar
13 votes

How to save Matrix data programmatically in Craft 3

Just an additional note: If you want to append certain elements to your matrix field instead of overwriting them all you have to do Update Craft 4.0 there is a new sortOrder key that makes my previous ...
Robin Schambach's user avatar
13 votes
Accepted

Getting info on the currently logged in user

Craft::$app->getUser() returns only the user service/component. You should read the docs about that What you want is the current Identity Craft::$app->getUser()->getIdentity()
Robin Schambach's user avatar
13 votes

How do you set a flash message in the template in Craft 3?

You can set flash messages using yii\web\Session::setFlash(). The session component is available to templates via craft.app.session. {% do craft.app.session.setFlash('error', 'You’re not allowed to ...
Brandon Kelly's user avatar
13 votes

Events documentation for Craft 3

Whilst Doug's answer is useful for knowing the ins and outs of a particular event once you've found it, actually finding which events are available in Craft 3 in the current docs is not straight-...
James Smith's user avatar
  • 5,304
12 votes
Accepted

Plugin logging in Craft 3

You can use Craft::getLogger()->log($message, $level, $category = 'application') It uses the Yii2 logger, you can change the $category to your plugin handle Edit: You can use the "short" syntax ...
Robin Schambach's user avatar
12 votes
Accepted

Craft 3 Redactor Link no longer has target option

Add the linkNewTab config option to the Redactor config you're using: So, that would become something like: { "buttons": [...], "plugins": [...], "linkNewTab": true }
Paul's user avatar
  • 6,338
12 votes
Accepted

User events in Craft 3

There aren't specific User element save events in Craft 3, but you can listen to EVENT_AFTER_SAVE_ELEMENT and check if the element is a user like so: Event::on(\craft\services\Elements::class, \craft\...
Brad Bell's user avatar
  • 67.5k
12 votes
Accepted

Is there a way to apply project.yaml changes on deployment?

You can create a script to use the craft executable to sync your project config file ./craft project-config/sync
Sam's user avatar
  • 308
11 votes
Accepted

Craft 3 RC Upgrade killed the WYSIWYG Editor

With RC1 the rich text editor was moved to a plugin. You can find the Redactor editor (the previous richtext field) in the plugin store along with a new CKEditor editor as well. You can read about ...
Tim Knight's user avatar
  • 1,022
10 votes
Accepted

Craft 3 plugins - Logging in a separate *.log file

Edit 2 I came up with a far far far better approach after a little brainstorming. I'll keep my old answer here but you should probably just use the first snipped. In your main Pluginfile.php public ...
Robin Schambach's user avatar
10 votes
Accepted

How to fetch entries in custom plugin Craft 3?

For instance like this: $entries = Entry::find() ->section('sectionName') ->orderBy('fieldName') ->limit(null) ->all(); Have a look here in the documentation (for the ...
KSP's user avatar
  • 849
10 votes
Accepted

URI for nested categories in Craft 3

You can actually use a bit of Twig logic to determine your category URI formats. Try this for your Category URI Format: {parent ? parent.uri : 'what-we-do'}/{slug} Which is just a more compact way ...
Alex Roper's user avatar
  • 2,632
10 votes

How do you set a flash message in the template in Craft 3?

Edit: Brandon replied as I was writing this. Feel free to remove :) You can do that using this in your conditional, just above your redirect tag {% do craft.app.session.setFlash('error', 'no no no') ...
Oli's user avatar
  • 7,495
10 votes

"Unknown package has no name defined" composer error on upgrade

The problem seems to be with parsing vendor/composer/installed.json Once I deleted it - install went well. Even without deleting other files. This one is generated one. You can also simply remove ...
Zebooka's user avatar
  • 201
9 votes

New Project: Wait for Craft 3 or start now with Craft 2?

Ben's answer is excellent, and should be marked as correct. This is simply some additional information to reinforce the decision to use Craft 2. The current version of Craft 3 is a developer preview ...
Lindsey D's user avatar
  • 24k
9 votes
Accepted

How to link to controller actions in a Craft 3 plugin?

Apparently I figured it out. Would still like a better way of doing this, but here's the solution I found to be working: In your Plugin class, you have to add a public $controllerMap array. For my ...
Gustavs's user avatar
  • 406
9 votes
Accepted

Craft 3: how to show the Yii Debug Toolbar?

There are two checkboxes in your user settings, go to your admin account -> preferences index.php?p=admin/myaccount and check the boxes This is covered in detail in the Profiling your Website with ...
Robin Schambach's user avatar
9 votes

Craft 3 plugins - Logging in a separate *.log file

Another perhaps simpler approach is to write to your own log file without using the Craft logger at all. $file = Craft::getAlias('@storage/logs/pluginhandle.log'); $log = date('Y-m-d H:i:s').' '.$...
Ben Croker's user avatar
  • 7,526
9 votes
Accepted

How to set up multi-env in Craft 3 with .env files?

These are the two approaches I usually choose from: Option 1: the 'default' Craft 3 way Create a .env file for every environment you create, and add it to your .gitignore file (if it's not yet in it)...
Paul's user avatar
  • 6,338

Only top scored, non community-wiki answers of a minimum length are eligible