Two small things this time:
1) Give some more info about the module from yesterday - I was a little tired when I wrote the post, so I didn't go into as much detail as I wanted to
2) Add a custom 404-page - this helps keep customers at your site, instead of showing them your hosts default 404-page.
So let's have a look at the module. I've created a new module for the description of the product and this is what my module-file looks like:
Code: Select all
class pi_description extends abstract_module {
const CONFIG_KEY_BASE = 'PI_DESCRIPTION_';
public $group = 'pi_modules_c';
public $content_width;
public function __construct() {
parent::__construct();
$this->group = basename(dirname(__FILE__));
$this->description .= '<div class="alert alert-warning">' . MODULE_CONTENT_BOOTSTRAP_ROW_DESCRIPTION . '</div>';
$this->description .= '<div class="alert alert-info">' . cm_pi_modular::display_layout() . '</div>';
if ( $this->enabled ) {
$this->group = 'pi_modules_' . strtolower(PI_DESCRIPTION_GROUP);
$this->content_width = (int)PI_DESCRIPTION_CONTENT_WIDTH;
}
}
public function getOutput() {
$products_description = $GLOBALS['product']->get('description');
if (!Text::is_empty($products_description)) {
$tpl_data = ['group' => $this->group, 'file' => __FILE__];
include 'includes/modules/block_template.php';
}
}
protected function get_parameters() {
return [
'PI_DESCRIPTION_STATUS' => [
'title' => 'Enable Model',
'value' => 'True',
'desc' => 'Should this module be shown on the product info page?',
'set_func' => "Config::select_one(['True', 'False'], ",
],
'PI_DESCRIPTION_GROUP' => [
'title' => 'Module Display',
'value' => 'C',
'desc' => 'Where should this module display on the product info page?',
'set_func' => "Config::select_one(['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I'], ",
],
'PI_DESCRIPTION_CONTENT_WIDTH' => [
'title' => 'Content Width',
'value' => '12',
'desc' => 'What width container should the content be shown in?',
'set_func' => "Config::select_one(['12', '11', '10', '9', '8', '7', '6', '5', '4', '3', '2', '1'], ",
],
'PI_DESCRIPTION_SORT_ORDER' => [
'title' => 'Sort Order',
'value' => '320',
'desc' => 'Sort order of display. Lowest is displayed first.',
],
];
}
}
The first part:
Code: Select all
class pi_description extends abstract_module {
const CONFIG_KEY_BASE = 'PI_DESCRIPTION_';
Here I've renamed "model" and "MODEL" to "description" and "DESCRIPTION".
The CONFIG_KEY_BASE is used to fetch the language constants, so you can see the title and description of the module, when you are configuring it from admin. It may be used for other things as well, but I don't know for sure. Thus, if the title and description does not show in admin, check that this value is the same as what comes before "TITLE" and "DESCRIPTION" in your language file.
The next couple of things, I haven't touched at all, but the function getOutput() is rather important. I haven't done much to it here as I'm just showing things as they are in the database, but sometimes, more is needed here.
Code: Select all
$products_description = $GLOBALS['product']->get('description');
$GLOBALS['product']->get fetches data from the product class, which automatically fetches everything asked for from the products and the products_description tables for the product in the database (probably a few tables more, like products_images). The field in products_description is actually called 'products_description', but Phoenix knows to look for things with 'products_' in front of it, so 'description' is enough to get the data I want.
If I wanted to show the weight it would have been: $GLOBALS['product']->get('weight').
If I wanted to show the supplier it would have been: $GLOBALS['product']->get('supplier') and so on.
Obviously this will sometimes just be an id, as is the case with manufacturers or the suppliers, and customers wouldn't have much joy from an id, so in that case, extra code to fetch the data related to that id would be needed.
Code: Select all
if (!Text::is_empty($products_description)) {
This line (and the closing '}' later on) checks if there is any data, and if there is, it will include the template-file and the output in the template file will be shown
My template file is vey basic:
Code: Select all
<div class="col-sm-<?= (int)PI_DESCRIPTION_CONTENT_WIDTH ?> pi-description">
<h5> <?= PI_DESCRIPTION_PUBLIC_TITLE ?> </h5>
<?= $products_description ?>
</div>
The output is placed in a div with the size defined as the content-width and with a css-class named pi-description, to allow direct control over this content through css.
Next, I added a title for the description, which is defined in the language file.
Lastly, I output the variable defined in the module file, so it actually shows the description and then I close the opening div.
To finish this explanation, I'll touch on the get_parameters() function, in the bottom of the module file:
Code: Select all
'PI_DESCRIPTION_STATUS' => [
'title' => 'Enable Description',
'value' => 'True',
'desc' => 'Should this module be shown on the product info page?',
'set_func' => "Config::select_one(['True', 'False'], ",
],
'PI_DESCRIPTION_GROUP' => [
'title' => 'Module Display',
'value' => 'C',
'desc' => 'Where should this module display on the product info page?',
'set_func' => "Config::select_one(['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I'], ",
],
'PI_DESCRIPTION_CONTENT_WIDTH' => [
'title' => 'Content Width',
'value' => '12',
'desc' => 'What width container should the content be shown in?',
'set_func' => "Config::select_one(['12', '11', '10', '9', '8', '7', '6', '5', '4', '3', '2', '1'], ",
],
'PI_DESCRIPTION_SORT_ORDER' => [
'title' => 'Sort Order',
'value' => '320',
'desc' => 'Sort order of display. Lowest is displayed first.',
],
These are what will be put in the database when the module is installed, and for a PI-module these four are the minimum:
- Status to toggle the module on and off while preserving the settings.
- Group to put the module into the wanted group.
- Content Width to decide how much horizontal space of the group this module is allowed to take up.
- Sort Order to make it show in the correct order.
Each of those items have a minimum of three keys that needs to be set:
- title, so you know what configuration you are looking at
- value, to give a default value this module will have when installed
- desch, to give further clarifications on what this actually controls and does
Some (like Content Width, Group and Status) also have a set_func, which shows the following values as options to choose from with a radio-button. Other things like this exist, to show a dropdown instead or to allows multiple entries to be selected with check-boxes.
If you need more things to be configurable in the module, it's easy to add another item to the list fx:
Code: Select all
'PI_DESCRIPTION_EXAMPLE' => [
'title' => 'Example',
'value' => '1337',
'desc' => 'This example does nothing, but could be a max-length of the description, before it's hidden with a Read More-thingy',
],
The above would do nothing with the module as is, so some code using the constant PI_DESCRIPTION_EXAMPLE should be written to make it do whatever you want it to do.
You could also add your own set_func values and then write the code to act on whatever is chosen.
And now for something completely different:
A custom 404-page is a page that will show, if someone tries to visit a non-existing page on your site. I often use mydomain.dk/pirat, as that is a very unlikely page to exist ("pirate"). To make your own 404-page, add this to .htaccess:
Code: Select all
#Custom 404
ErrorDocument 404 /404.php
create a page called 404.php (you could also change 404.php in .htaccess to something else like "ooops.php" and then make a page with that name instead), and make it show whatever you want it to.
I decided to make mine controlled by the InfoPages-system, so I copied shipping.php, templates/default/includes/pages/shipping.php and includes/languages/$language/shipping.php and renamed them to 404.php. In the templates/default/includes/pages/404.php I changed line 14:
and line 18:
Code: Select all
$breadcrumb->add($page['pages_title'], $Linker->build('404.php'));
Next I used admin->Tools->InfoPages to create a new page with the slug '404', made it 'Not Published' and wrote some text. Now this is my 404-page:
404.png
//Daniel
You do not have the required permissions to view the files attached to this post.