17
votes
Display a copyright year (or range)
You can use Twig's date filter:
{{ "now"|date('Y') }} Sitename. All rights reserved.
More info in the Twig docs
- 598
13
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.
- 566
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
* @...
- 351
10
votes
Accepted
Display a copyright year (or range)
The first two lines of code determine the start year and the current year.
The next block of code compares the two years, and sets the copyrightYears variable accordingly.
Finally, we simply output ...
- 23.8k
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 ...
- 67k
8
votes
Accepted
How can I check from a plugin config file if devMode is enabled?
It's as simple as
if ( craft()->config->get( 'devMode' ) )
{
//do stuff
}
- 3,843
6
votes
Accepted
How do I create an advanced route with multiple slugs?
Brad nails the problem in his comment:
The error is because you've got two named subpatterns with the same name (<slug>)
And the solution is simple... Change the name(s) of your subpattern(s)...
- 23.8k
6
votes
Accepted
Update entry.body from Plugin
There is no event or hook that I'm aware of that can intercept the template rendering process directly. You would likely need to instigate this from within your template. I would suggest perhaps using ...
- 13.4k
6
votes
Accepted
Plugin Routes: Asterisk Token effect from CP Routes
You can't have tokens in your plugin routes, but you can easily add a segment variable that recreates the asterisk token's functionality with some regex. According to the the official docs on routing, ...
- 21.7k
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',
- 21.7k
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 ...
- 21.7k
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 ...
- 67k
6
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 ...
- 67k
5
votes
Accepted
Get Groups from User in plugin
Your code to get a user returns a UserModel (as can be seen from the Full Class Reference docs):
$user = craft()->users->getUserById($id);
You can then get the user groups the $user is a ...
- 3,298
5
votes
Upload any size image into carousel
You shouldn't need to create a custom plugin to do that.
You can easily create a Matrix content field for your carousel. Create a new block called slide. From there you can create an asset field to ...
- 572
5
votes
Accepted
Are there any advantages of updating from PHP 5.4.40 to 5.6?
Short yes! You can find a detailed Benchmark on http://www.lornajane.net/posts/2014/php-5-6-benchmarks. If possible i would try to update from PHP 5.4.40 direct to PHP 7. The performance from PHP 5.4 ...
- 1,327
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>
- 23.8k
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 ...
- 3,109
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": {
"...
- 269
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 ...
- 7,081
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
{{ ...
- 106
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,...
- 1,741
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();
$...
- 661
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?
- 7,340
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....
- 3,109
4
votes
Accepted
Plugin Settings: Attribute Types, and how to receive
As far as accessing the value of the setting, try:
$plugin = craft()->plugins->getPlugin('pluginHandle');
$plugin->getSettings()->getAttribute('pluginSettings1');
If you need to access ...
- 13.4k
4
votes
Accepted
Dynamic routing via plugin with special characters
You'll need to update the regex to allow for additional characters. For example, if you want to allow !, then this will work:
return array(
'myroute/(?P<myVariable>[-\w\!]+)' => array('...
- 67k
4
votes
Accepted
Twig Filter in Plugin/PHP?
In one of your plugin services, you can render a template that handles the markdown filter and return the results as an HTML string. You can then send that HTML to wherever you want to preview.
In ...
- 3,821
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, ...
- 1,266
4
votes
Set Structure Parent when creating Entry in Plugin
This question is old, but the solution is:
// look up parent with a ElementCriteriaModel
$parentCriteria = craft()->elements->getCriteria(ElementType::Entry);
$parentCriteria->search = '...
- 631
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
php × 289craft3 × 78
plugin-development × 70
templating × 23
plugins × 21
assets × 14
plugin-elementapi × 12
error-message × 12
config × 11
error × 10
modules × 10
yii × 10
plugin-craftcommerce × 9
install × 9
server × 9
composer × 8
entries × 7
upload × 7
installation × 7
events × 6
queue × 6
database × 5
entry × 5
cache × 5
controller × 5