Commit 6392f4c3 authored by nicola85's avatar nicola85

Improve field handlers

parent 3e574d9a
......@@ -9,3 +9,4 @@ dependencies[] = rest_server
dependencies[] = services
files[] = includes/bat_api.bat_unit_field_handler.inc
files[] = includes/bat_api.bat_type_field_handler.inc
......@@ -27,6 +27,10 @@ function bat_api_ctools_plugin_directory($owner, $plugin_type) {
if ($owner == 'bat_api' && $plugin_type == 'bat_unit_field_handler') {
return 'plugins/bat_unit_field_handler';
}
if ($owner == 'bat_api' && $plugin_type == 'bat_type_field_handler') {
return 'plugins/bat_type_field_handler';
}
}
/**
......@@ -37,6 +41,9 @@ function bat_api_ctools_plugin_type() {
'bat_unit_field_handler' => array(
'use hooks' => TRUE,
),
'bat_type_field_handler' => array(
'use hooks' => TRUE,
),
);
}
......@@ -1164,7 +1171,7 @@ function bat_api_services_types_index($type_ids, $offset, $limit) {
$return->sessid = session_id();
ctools_include('plugins');
$field_handlers = ctools_get_plugins('bat_api', 'bat_unit_field_handler');
$field_handlers = ctools_get_plugins('bat_api', 'bat_type_field_handler');
$ids = array_filter(explode(',', $type_ids));
......
<?php
interface BatTypeFieldHandlerInterface {
public function getFieldType();
public function getFieldInfo();
}
abstract class BatTypeFieldHandlerBase implements BatTypeFieldHandlerInterface {
protected $type;
public function __construct(BatType $type) {
$this->type = $type;
}
}
<?php
$plugin = array(
'label' => t('Commerce Price'),
'handler' => array(
'class' => 'BatTypeFieldHandlerCommercePrice',
),
);
class BatTypeFieldHandlerCommercePrice extends BatTypeFieldHandlerBase {
public function getFieldType() {
return 'commerce_price';
}
public function getFieldInfo() {
$price_fields = array();
$bat_type = bat_type_load($this->type->type);
$fields = field_info_instances('bat_type', $this->type->type);
foreach ($fields as $field) {
$field_info = field_info_field($field['field_name']);
if ($field_info['type'] == 'commerce_price') {
$field_array = $this->type->{$field['field_name']};
if (isset($field_array[LANGUAGE_NONE][0]['amount'])) {
$price_fields[$field['field_name']] = array(
'amount' => $field_array[LANGUAGE_NONE][0]['amount'],
'currency_code' => $field_array[LANGUAGE_NONE][0]['currency_code'],
);
}
}
}
return $price_fields;
}
}
......@@ -16,12 +16,6 @@ class BatUnitFieldHandlerCommercePrice extends BatUnitFieldHandlerBase {
public function getFieldInfo() {
$price_fields = array();
$bat_unit_type = bat_type_load($this->unit->type);
$base_price_field = NULL;
if (isset($bat_unit_type->data['pricing_field'])) {
$base_price_field = $bat_unit_type->data['pricing_field'];
}
$fields = field_info_instances('bat_unit', $this->unit->type);
foreach ($fields as $field) {
$field_info = field_info_field($field['field_name']);
......@@ -29,16 +23,11 @@ class BatUnitFieldHandlerCommercePrice extends BatUnitFieldHandlerBase {
if ($field_info['type'] == 'commerce_price') {
$field_array = $this->unit->{$field['field_name']};
if ($field['field_name'] != $base_price_field) {
if (isset($field_array[LANGUAGE_NONE][0]['amount'])) {
$price_fields[$field['field_name']] = array(
'amount' => $field_array[LANGUAGE_NONE][0]['amount'],
'currency_code' => $field_array[LANGUAGE_NONE][0]['currency_code'],
);
}
else {
$price_fields[$field['field_name']] = array();
}
if (isset($field_array[LANGUAGE_NONE][0]['amount'])) {
$price_fields[$field['field_name']] = array(
'amount' => $field_array[LANGUAGE_NONE][0]['amount'],
'currency_code' => $field_array[LANGUAGE_NONE][0]['currency_code'],
);
}
}
}
......
<?php
$plugin = array(
'label' => t('Base Price'),
'handler' => array(
'class' => 'BatUnitFieldHandlerBasePrice',
),
);
class BatUnitFieldHandlerBasePrice extends BatUnitFieldHandlerBase {
public function getFieldType() {
return 'base_price';
}
public function getFieldInfo() {
$price_fields = array();
$bat_unit_type = bat_type_load($this->unit->type);
$base_price_field = NULL;
if (isset($bat_unit_type->data['pricing_field'])) {
$base_price_field = $bat_unit_type->data['pricing_field'];
}
$fields = field_info_instances('bat_unit', $this->unit->type);
foreach ($fields as $field) {
$field_info = field_info_field($field['field_name']);
if ($field_info['type'] == 'commerce_price') {
$field_array = $this->unit->{$field['field_name']};
if ($field['field_name'] == $base_price_field) {
if (isset($field_array[LANGUAGE_NONE][0]['amount'])) {
$price_fields[$field['field_name']] = array(
'amount' => $field_array[LANGUAGE_NONE][0]['amount'],
'currency_code' => $field_array[LANGUAGE_NONE][0]['currency_code'],
);
}
else {
$price_fields[$field['field_name']] = array();
}
}
}
}
return $price_fields;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment