commerce_sdf.module in Commerce Stock 7.2
Provide a Decimal formater for converting stock levels into text messages.
File
modules/commerce_sdf/commerce_sdf.moduleView source
<?php
/**
* @file
* Provide a Decimal formater for converting stock levels into text messages.
*/
/**
* Implements hook_field_formatter_info().
*/
function commerce_sdf_field_formatter_info() {
$settings = array();
// Defaults for in and out of stock.
$settings['display'][0]['seuil'] = 0;
$settings['display'][0]['message'] = t('Out of stock');
$settings['display'][1]['seuil'] = 99999999;
$settings['display'][1]['message'] = t('In stock');
// Add empty values.
for ($i = 2; $i < 5; $i++) {
$settings['display'][$i]['seuil'] = '';
$settings['display'][$i]['message'] = '';
$settings['display'][$i]['classname'] = '';
}
// Return the formatter info.
return array(
'commerce_sdf_formatter' => array(
'label' => t('Show stock display as message'),
'field types' => array(
'number_decimal',
),
'settings' => $settings,
),
);
}
/**
* Implements hook_field_formatter_settings_summary().
*/
function commerce_sdf_field_formatter_settings_summary($field, $instance, $view_mode) {
$display = $instance['display'][$view_mode];
$settings = $display['settings'];
if ($display['type'] == 'commerce_sdf_formatter') {
$display = $instance['display'][$view_mode];
$settings = $display['settings'];
$summary = '';
foreach ($settings['display'] as $row => $seuilstr) {
$seuil = $seuilstr['seuil'];
if ($seuil !== '') {
// @codingStandardsIgnoreStart
$summary .= t('Print @message when stock is <= @seuil.', array(
'@seuil' => $settings['display'][$row]['seuil'],
'@message' => $settings['display'][$row]['message'],
)) . "<br />";
// @codingStandardsIgnoreEnd
}
}
return $summary;
}
}
/**
* Implements hook_field_formatter_settings_form().
*/
function commerce_sdf_field_formatter_settings_form($field, $instance, $view_mode, $form, &$form_state) {
$display = $instance['display'][$view_mode];
$settings = $display['settings'];
$element = array();
if ($display['type'] == 'commerce_sdf_formatter') {
$element['help'] = array(
'#markup' => 'You can use the token @stock as part of the Message (i.e. "only @stock in stock")',
'#prefix' => '<div>',
'#suffix' => '</div>',
);
$element['display'] = array(
'#markup' => '',
'#prefix' => '<table style="width:100%;">',
'#suffix' => '</table>',
'#tree' => TRUE,
);
$element['display']['header'] = array(
'#markup' => '<thead>
<tr>
<th style="width:150px"> </th>
<th style="width:100px">Seuil</th>
<th style="width:300px">Message</th>
<th style="width:200px">classname</th>
</tr>
</thead>',
);
for ($i = 0; $i < 5; $i++) {
$element['display'][$i] = array(
'#prefix' => '<tr>',
'#suffix' => '</tr>',
);
$element['display'][$i]["text"] = array(
'#markup' => '<td style="width:150px">If stock <=</td>',
'#size' => 10,
);
$element['display'][$i]["seuil"] = array(
'#type' => 'textfield',
'#default_value' => $settings['display'][$i]['seuil'],
'#size' => 5,
'#disabled' => $i == 0,
'#prefix' => '<td>',
'#suffix' => '</td>',
);
$element['display'][$i]["message"] = array(
'#type' => 'textfield',
'#default_value' => $settings['display'][$i]['message'],
'#prefix' => '<td>',
'#size' => 30,
'#suffix' => '</td>',
);
$element['display'][$i]["classname"] = array(
'#type' => 'textfield',
'#size' => 20,
'#default_value' => $settings['display'][$i]['classname'],
'#prefix' => '<td>',
'#suffix' => '</td>',
);
}
}
return $element;
}
/**
* Implements hook_field_formatter_view().
*/
function commerce_sdf_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
// Get the settings.
$settings = $display['settings'];
// Initialize the var.
$element = array();
$stock = round($items[0]['value']);
$anc_seuil = -9999999;
foreach ($settings['display'] as $row => $seuilstr) {
$seuil = $seuilstr['seuil'];
if (is_numeric($seuil)) {
if ($stock > $anc_seuil && $stock <= $seuil) {
if (isset($seuilstr['classname'])) {
// @codingStandardsIgnoreLine
$element[0]['#markup'] = "<span class='" . $seuilstr['classname'] . "'>" . t($settings['display'][$row]['message'], array(
"@stock" => $stock,
)) . "</span>";
}
else {
// @codingStandardsIgnoreLine
$element[0]['#markup'] = "<span>" . t($settings['display'][$row]['message'], array(
"@stock" => $stock,
)) . "</span>";
}
break;
}
$anc_seuil = $seuil;
}
}
return $element;
}
Functions
Name | Description |
---|---|
commerce_sdf_field_formatter_info | Implements hook_field_formatter_info(). |
commerce_sdf_field_formatter_settings_form | Implements hook_field_formatter_settings_form(). |
commerce_sdf_field_formatter_settings_summary | Implements hook_field_formatter_settings_summary(). |
commerce_sdf_field_formatter_view | Implements hook_field_formatter_view(). |