14
votes
How can I check from a plugin config file if devMode is enabled?
For those coming here using Craft 3, it's now Craft::$app->getConfig()->general->devMode.
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
* @...
9
votes
Accepted
PHP notice unserialize(): Unexpected end of serialized data
Update:
Craft 2.6.2957 added support for PHP 7.1
There's a lot going on here, but 1st thing is 1st.
The vagrant box is running PHP 7.1
Craft 2 (and the current latest version of the Yii ...
7
votes
Upload file size limits to 256MB
When determining the maximum upload file size, Craft first takes the php.ini file upload_max_filesize, post_max_size and memory_limit into account by getting the minimum value and only then applying ...
6
votes
Accepted
Adding new locale - hangs at Step 3 'ResaveAllElements'
You probably need to up Craft's internal memory limit, as well – the following should go into the array in your general.php configuration file:
'phpMaxMemoryLimit' => '1024M',
6
votes
Accepted
Where can I run custom PHP scripts?
If you're simply looking to execute some custom scripts that are completely standalone (i.e. they don't interact with the Craft application in any way), those files should live somewhere below your ...
6
votes
CraftCMS > UserSessionService should be compatible with CWebUser
It's a known issue you can safely ignore on Craft 2, that's been resolved for Craft 3.
PHP 7.0 (or maybe it was 7.1) changed the way that particular error was reported from E_STRICT (which Craft 2 is ...
5
votes
Getting Twig_Error_Runtime when trying to open the website
siteName is automatically global... you don't need to include the global. prefix.
<title>{{ siteName }}</title>
5
votes
Accepted
How do I "register" a service in a Craft 3 plugin?
It turns out there's a handy method for manually registering your services, or in Yii2 parlance, just "components":
namespace example\myplugin;
use example\myplugin\services\Example as ...
5
votes
How do I "register" a service in a Craft 3 plugin?
You can do this in Composer with the following:
"extra": {
"name": "My Plugin",
"handle": "my-plugin",
"hasCpSettings": false,
"hasCpSection": false,
"components": {
"...
5
votes
Accepted
How can I get tasks to complete when not logged in to the CP?
To reiterate what has been said in the comments above and to present an answer, the best way to ensure that queue jobs are completed even without the CMS being logged into or the site being visited is ...
5
votes
Accepted
How to access HTML input values from Twig and vice versa
You can do it without JavaScript (if you don't mind the page refreshing) by accessing the request variable:
https://docs.craftcms.com/api/v3/craft-web-twig-variables-request.html#public-methods
{{ ...
5
votes
Accepted
Local DB SQL 'CDbException' Errors on Craft 2 site
this is a MySQL/i version issue on Craft 2 sites.
You'll need to add this to your database config (craft/config/db.php):
'initSQLs' => array("SET SESSION sql_mode='STRICT_TRANS_TABLES,...
5
votes
Accepted
Craft 3 plugin - how to update entry with field of Entries type
Ok this was easy. Tried it at first but didn't work and was successful after another go. All it needed was a array of Entry IDs.
Working code:
$entry = Entry::find()->id($entryId)->one();
$...
5
votes
Accepted
Undefined index: siteSettings when updating entry types
Even with 'useProjectConfigFile' => false, it is still active under the hood.
Could you run ./craft project-config/rebuild and see if that fixes the issue?
5
votes
Accepted
Is there a way to compare what has changed on an entry while it's being saved?
You're probably looking for Element::getDirtyAttributes() and Element::getDirtyFields()!
The former is for native class properties/attributes (and is based on an ActiveRecord pattern in Yii, I believe....
4
votes
Can we use PHP in our templates instead of Twig?
In craft 3 it is actually possible.
{{ craft.app.view.evaluateDynamicContent('phpinfo();') }}
As you can see above, php code is passed as string to evaluateDynamicContent function. On inside, ...
4
votes
Accepted
Monitor PHP memory usage
If you enable devMode, then memory usage, along with tons of other debugging information (# of database queries, query profiles, execution timestamps, etc.) will be output to your browser's console.
4
votes
Need help with simple PHP plugin
I think you can do this in the template by using for example:
craft.request.getSegments
or
craft.request.getParam( name )
See the docs:
https://craftcms.com/docs/templating/craft.request
4
votes
Accepted
Craft Commerce update variant field in plugin
This is the general mechanism for updating custom fields on elements - use setContentFromPost and then call the element's save function.
$variant->setContentFromPost(array(
'...
4
votes
Accepted
Installing Craft without MAMP
It depends what you are most comfortable with. PHP and Apache are shipped with macOS by standard but you'd still need to install and configure MySQL (or MariaDB etc), along with manual virtual host ...
4
votes
Accepted
Use pure PHP and HTML instead of Templates
You're really fighting against the way Craft CMS does things.
I'd start by having a good read through the Craft Docs first, there really isn't a shortcut. The docs are really great though, and ...
4
votes
Twig Cycle - how to show different html based on cycle
I tried this and this worked out also:
{% if loop.index is even %}
<div></div>
{% endif %}
{% if loop.index is odd %}
<div></div>
{% endif %}
4
votes
Returning an array to Twig from a plugin
Your plugin should be able to handle PHP arrays just fine.
craft/plugins/myplugin/variables/MyPluginVariable.php
function getArray()
{
return array('hi', 'there');
}
craft/templates/template....
4
votes
Accepted
Upgraded to PHP 7.1 - GD or ImageMagick is required
You'll need to contact your hosting provider. There are any number of ways that this "PHP switching" can be implemented, but typically each major PHP version has its own separate modules/config files ...
4
votes
Accepted
PHP Fatal error during installation
See this issue: https://github.com/craftcms/cms/issues/4355#issuecomment-498829513
reproduced here for completeness:
A yii variable 'isWindows' is apparently the way to correct this issue, but I ...
4
votes
ext-zip trouble
None of various attempts to get ext-zip going under Catalina had worked for me, including the above, but here’s what did, with thanks to Chad for showing the essence of the issue:
“the Craft system ...
4
votes
Programmatically add entries to an Entry field (craft\fields\Entries)
If you are running on Craft 3, as of 3.4.0 (and really since the release of craft 3), setting custom field values directly like
$entry->customField = [id1, id2, id3];
is broken. You should ...
4
votes
Accepted
Programmatically add entries to an Entry field (craft\fields\Entries)
If you use Craft::$app->getGlobals()->saveSet($reglages); you are saving the Global's itself (its settings), not its content.
Here, you should use Craft::$app->elements->saveElement($...
4
votes
Accepted
Set parent-child relationship in PHP
Just to clear up a potential source of confusion: For Craft, the only 'real' parent-child relationships are the hierarchies in Structures (a type of Section). Entry fields and by extension relations (...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
php × 301craft3 × 81
plugin-development × 71
templating × 24
plugins × 21
assets × 14
error-message × 14
plugin-elementapi × 13
config × 11
modules × 11
error × 10
server × 10
yii × 10
plugin-craftcommerce × 9
install × 9
composer × 9
entries × 8
craft2 × 7
upload × 7
installation × 7
database × 6
events × 6
queue × 6
entry × 5
cache × 5