18

I would create a Single with the URI set to "404", and also set its Template to "404". That entry will automatically get loaded if you go to http://example.com/404, and load your 404.html template, so at the top of the template you should add this to it: {# # Fetch the 404 Single entry, if we don’t already have it. # (requests to /404 will already have ...


9

I'm not familiar with EE's show_error, but a plugin's error template logic is identical to the logic that Craft uses to determine what to display. From your plugin, if you: throw new HttpException($statusCode, $optionalMessage) Where $statusCode is 404 or 400, or any proper http status code, Craft will load the corresponding 404.html, 400.html, etc. from ...


5

Yes — an error.twig template in your error templates directory serves as a catch-all template when a more specific template (e.g. 404.twig) can't be found, or when the system throws a general error that doesn't have a status code attached. (You can customize the path prefix for error templates by changing the errorTemplatePrefix setting in the Craft General ...


5

If you use $model->getErrors() instead, it should return a multidimensional array of errors indexed by attribute name like so: array( 'email' => array( 'First email error.', 'Second email error.', ), 'firstName' => array( 'First firstName error.', ), ); Or you can get errors specifically for an attribute ...


4

I just return the form model object back to the template. Yii has built in validation and error handling within the models themselves. In the example email is the instance of your form's model. The template: {% if email is not defined %} {% set email = craft.postmaster.email() %} {% endif %} {% for error in email.getAllErrors() %} <p>{{ ...


4

A big thanks to Michael Rog (@rog) for the answer on Slack. You can create a catch-all error template with the name error (using whatever file ext works for you). That template also has access to a variable called code that tells you what kind of error it is. In my case, I'm using it to display HTTP Cats.


3

Craft will serve one of 3 types of 404 errors. When Dev Mode is enabled, you will get something like this, which shows the full stack trace leading up to the NotFoundHttpException: If this looks like what you’re getting, then Dev Mode is definitely enabled. Double-check config/general.php for devMode, or web/index.php for YII_DEBUG. Otherwise, you will ...


3

There's a long discussion of all of this in the Handling Errors Gracefully in Craft CMS article, if it helps.


3

As it turns out, I had made a configuration change that broke Craft's default behavior: 'defaultTemplateExtensions' => array('twig', 'xml') All of Craft's internal templates use .html, and HTML templates are included in the default value of that config item. By overriding it, I had rendered Craft unable to find its stock error template. Moral of the ...


3

For whatever reason, your site isn't able to find the image. And since your Twig code isn't being defensive, it's failing ungracefully. {% set image = block.media.first() %} This first part is fine... I'm assuming that media is an Assets relationship field. You're attempting to fetch the first asset, and set that to image. Here's the kicker... when no ...


3

Hadn't thought about using simple if logic here in the template {% if entry.postDate <= now|date_modify('-90 days') %} {% redirect 'jobs/expired' %} {% endif %}


3

Try this: {% set category = craft.categories(segmentCategory).first %} Without the first at the end, you're setting category to an ElementCriteriaModel. Which would be fine in many cases, but it's not an acceptable variable type for targetElement. Ditto for the sourceElement: craft.entries.section(segmentEntryType).status(null).first


3

You will want to include the siteUrl variable before the file path to normalize all the resource URLs. That will also help when you move to production. For example: {% includeCssFile siteUrl~'assets/html/css/slidestyle.css' %} In your general config file, you can set different site URLs based on the server you are on. return array( '*' => array(), ...


2

Craft itself (I'm referencing the EntriesController) checks to see if an entry is saved. If it doesn't it, points back to the template and hands it an error message in the session and any needed variables. In the case of your plugin, gather your form data and errors and send it back. Craft does it all in a single Model. You can do the same or different. It ...


2

This will do it, and you can change the 400 to whatever type of http error you want: throw new HttpException(400, 'Error message text');


2

404 redirect: You could set up your single to use 404-not-found (example) as a uri, and add a redirect: {% redirect '/404-not-found' %} to the /404.html file. That way every 404-page gets redirected to the single. Downside: You will loose the 404-header, and the uri in the address-bar will be lost. . Load Single: Set up a single, and load the single ...


2

You can do this using Craft's static translation support. For example, if your site is using en_us for its primary locale, then add a craft/translations/en_us.php file that has this in it: <?php return array( '{attribute} "{value}" has already been taken.' => '{attribute} "{value}" has already been taken. Login here instead.', );


2

Well I now see this error was caused by the included template and not the fact that I was trying to include a template. I thought that the error message would show the error on the included template that was the actual source of the problem, but it was just referencing the include as the error.


2

For SEO purposes, it might actually be better to keep the listing live, but clearly mark it as expired. What I ended up doing was just having an additional field in my section that defined my own internal status of the entry, and then adjusted the display based on that. If simple expiration is enough for you, then it probably doesn't matter, though.


2

Perhaps instead of trying to get a template name, use the entry type as the template name, similar to the method that you linked to. In the link, it doesn't check which template is assigned to an entry, but instead, uses the entry type to load a template with the same name. In your example that might look like this: craft()->templates->render('path/to/...


2

It's a bit hard to tell from this snippet, but if image is an asset field it's important to remember that these are returned as arrays so you have to access the url more like block.image.first().url. However, if the image variable is set elsewhere this probably won't help. EDIT: Relevant docs


2

Take a look in your general.php config file and see if you're overwriting the errorTemplatePrefix setting. It might look something like: 'errorTemplatePrefix' => "_errors/" So you might need to move the 404.html file into "/templates/_errors/" directory. See docs v3


2

I believe I already answered this in the OP's other, similar question, but: You shouldn't pass a string value (i.e. 'posts') to getNext() and getPrev() – those methods expect either an ElementCriteriaModel instance, or an object with attributes representing a query (i.e. something like { section: 'posts' }. What actually happens when you call entry.getPrev(...


2

If you've setup a channel, it does not include a "blog index" by default, for this you need to make it yourself by placing an index.twig file within the blog folder (If that is a directory in your templates folder). Check the answer in this question for an example on how to do the twig code. What's the right way to create a simple blog in Craft?


2

If blog is a channel and site.com/blog/blog-post-name is correctly displayed but you can't access site.com/blog, that is probably because you are missing an index.html template inside the blog folder. Craft will do the route automatically if you add an index.html file inside a section folder. (rightly named) Then, what you want is to display the blog ...


1

Take a look at this: https://craftcms.com/docs/templating/paginate I use this all the time and it will work then.


1

The good news in the upcoming Craft 3 this isn't an issue. 404's get logged to their own separate log files and won't clutter up the main Craft log files specifically for this reason. In Craft 2, I think you'd have to do something like write a plugin that listens for Yii's onException event, makes sure it's a 404 status code, then go through Yii's CLogger ...


1

Turns out it was HHVM causing the issue. Switching back to 'normal' PHP made the problem disappear.


1

I don't believe there is any 'built in code' you could use. You have 2 options though. You could either do this in twig by checking all fields in a tab and adding an error class to the tab: {% set actionClasses = "" %} {% if (sale.getErrors('discountAmount') or sale.getErrors('discountType')) %} {% set actionClasses = "error" %} {% endif %} {% set ...


1

There is not a built-in way. If you take a look at the UserSessionService, you'll see that messages are returned based on an error code. When a username and password combination fail, it triggers this single message to be returned.


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