Applicazione patch per formattazione campo decimale
Vorrei applicare questa patch https://www.drupal.org/node/2451775
il link diretto è: https://www.drupal.org/files/issues/math_exp_unformat_token-2451775.patch
il risultato è:
<code><?php
/**
* @file
* Definition of views_handler_field_math.
*/
/**
* Render a mathematical expression as a numeric value
*
* Definition terms:
* - float: If true this field contains a decimal value. If unset this field
* will be assumed to be integer.
*
* @ingroup views_field_handlers
*/
class views_handler_field_math extends views_handler_field_numeric {
function option_definition() {
$options = parent::option_definition();
$options['expression'] = array('default' => '');
return $options;
}
function options_form(&$form, &$form_state) {
$form['expression'] = array(
'#type' => 'textarea',
'#title' => t('Expression'),
'#description' => t("Enter mathematical expressions such as 2 + 2 or sqrt(5). You may assign variables and create mathematical functions and evaluate them. Use the ; to separate these. For example: f(x) = x + 2; f(2). The result of the previous row's mathematical expression can be accessed by using the [expression] token itself."),
'#default_value' => $this->options['expression'],
);
// Create a place for the help
$form['expression_help'] = array();
parent::options_form($form, $form_state);
// Then move the existing help:
$form['expression_help'] = $form['alter']['help'];
unset($form['expression_help']['#dependency']);
unset($form['alter']['help']);
}
function render($values) {
ctools_include('math-expr');
/**
* Unformat number.
*
* @param string $number
* The formatted number.
* @param bool $force_number
* Force to convert to number or not.
* @param string $dec_point
* Decimal point.
* @param string $thousands_sep
* Thousand separator.
*
* @return string
* Unformat number.
*/
function number_unformat($number, $force_number = FALSE, $dec_point = '.', $thousands_sep = ',') {
if ($force_number) {
$number = preg_replace('/^[^\d-]+/', '', $number);
}
else {
if (preg_match('/^[^\d]+/', $number)) {
return FALSE;
}
}
$type = (strpos($number, $dec_point) === FALSE) ? 'int' : 'float';
$number = str_replace(array($dec_point, $thousands_sep), array(
'.',
''
), $number);
settype($number, $type);
return $number;
}
/**
* Unformat token value.
*
* @param string $token_value
* The token value.
* @param string $token_name
* The token name ([field_a]).
* @param object $values
* View result at each row.
*
* @return float
* Unformat token value.
*/
function unformat_token_value($token_value, $token_name, $values) {
$token_name = str_replace('[', '', $token_name);
$token_name = str_replace(']', '', $token_name);
if (!empty($this->view->field[$token_name])) {
$handler = $this->view->field[$token_name];
if (is_a($handler, 'views_handler_field_numeric')) {
// Remove link.
$token_value = $handler->original_value;
// Remove prefix and suffix.
$prefix = $handler->options['prefix'];
$suffix = $handler->options['suffix'];
$token_value = str_replace($prefix, '', $token_value);
$token_value = str_replace($suffix, '', $token_value);
// Unformat number.
$token_value = $this->number_unformat($token_value, TRUE, $handler->options['decimal'], $handler->options['separator']);
return floatval($token_value);
}
// Support for field commerce_price.
if (is_a($handler, 'commerce_price_handler_field_commerce_price')) {
$amount = $handler->get_value($values);
$amount = $amount[0];
return floatval(a_currency_amount_to_decimal($amount['amount'], $amount['currency_code']));
}
}
return $token_value;
}
function render($values) {
ctools_include('math-expr');
$tokens = $this->get_render_tokens(array());
foreach ($tokens as $token_name => &$token_value) {
$token_value = $this->unformat_token_value($token_value, $token_name, $values);
}
$value = strtr($this->options['expression'], $tokens);
$expressions = explode(';', $value);
$math = new ctools_math_expr;
$value = strtr($this->options['expression'], $tokens);
$expressions = explode(';', $value);
$math = new ctools_math_expr;
foreach ($expressions as $expression) {
if ($expression !== '') {
$value = $math->evaluate($expression);
}
}
// The rest is directly from views_handler_field_numeric but because it
// does not allow the value to be passed in, it is copied.
if (!empty($this->options['set_precision'])) {
$value = number_format($value, $this->options['precision'], $this->options['decimal'], $this->options['separator']);
}
else {
$remainder = abs($value) - intval(abs($value));
$value = $value > 0 ? floor($value) : ceil($value);
$value = number_format($value, 0, '', $this->options['separator']);
if ($remainder) {
// The substr may not be locale safe.
$value .= $this->options['decimal'] . substr($remainder, 2);
}
}
// Check to see if hiding should happen before adding prefix and suffix.
if ($this->options['hide_empty'] && empty($value) && ($value !== 0 || $this->options['empty_zero'])) {
return '';
}
// Should we format as a plural.
if (!empty($this->options['format_plural']) && ($value != 0 || !$this->options['empty_zero'])) {
$value = format_plural($value, $this->options['format_plural_singular'], $this->options['format_plural_plural']);
}
return $this->sanitize_value($this->options['prefix'] . $value . $this->options['suffix']);
}
function query() { }
} </code>
| Allegato | Dimensione |
|---|---|
| views_handler_field_math.txt | 2.99 KB |

Risposte
Ciao Tommy, non riesci ad
Ciao Tommy, non riesci ad applicarla oppure il problema è altro?
@tommy55 il file dove
@tommy55
il file dove applicarla è
handlers/views_handler_field_math.inc
dove c'è il segno + la riga va aggiunta
dove c'è il segno - la riga va tolta
Il migliore modo è applicare
Il migliore modo è applicare la patch con il comando patch disponibile su vari sistemi.
La sintassi è del tipo
patch < [nome del file]il comando ha vari switch, tra cui molto utile --dry-run per vedere le modifiche senza applicarle, in modo da capire se la patch è compatibile con la versione del file.
Ti allego il file patchato risultante da questa esecuzione
(nota il -p2 occorre solo perchè non ho lanciato il programma da dentro la directory del modulo)
views_handler_field_math.inc.txt
vedi se funziona correttamente.
Grazie a tutti per
Grazie a tutti per l'interessamento. Essendo inesperto in queste cose ho sempre applicato in modo manuale, come suggerisce motodesk. L'errore che mi dà è che non carica più le pagine con viste in cui è presente un campo globale con espressione mat.
Ora io uso un host condiviso, con pannello plextor c-panel che è comodissimo per molte cose. Immagino che dovrò trovare un'aplicazione che fa questo all'interno di c-panel stesso. Nel file manager è presente solo un editor di codice con le righe numerate. Credo mi manchi qualche fondamentale.
Se avete qualche altro suggerimento sono tutto orecchie.
Il fatto che le viste
Il fatto che le viste interessate non vengano più visualizzate può dipendere da vari fattori: la cosa migliore è consultare i log di Drupal alla ricerca di qualche errore e se il pannello te ne dà la possibilità consultare anche i log di errore prodotti da php/webserver. A primo impatto mi viene da pensare a qualche errore di sintassi nell'applicare la patch a mano oppure che la patch stessa non funzioni correttamente.
Prova a sostituire views_handler_field_math.inc con il file che ti ho passato e a pulire la cache dell'installazione. Se persiste la patch non funziona e l'unica via è ripristinare il file originale e postare sulla issue su Drupal.org il problema rilevato.