Basically I want to create a custom handler to unserialize a db field called birthday.
I've managed to correctly output the field serialized using the default views_handler_field. Unfortunately When I try to create a custom handler, I get this message:
Error: handler for drappsprofiles > birthday doesn't exist!
Here's the file structure:
all/modules/drapps/drappsprofile/
|->drappsprofiles.views.inc
|->drappsprofiles.module
|->drappsprofiles.install
|->drappsprofiles.info
|->drappsprofiles.inc
|->drappsprofiles_handler_field_birthday.inc
here's drappsprofiles.module
/**
* VIEWS2 MODULE
* Implementation hook_views_api
**/
function drappsprofiles_views_api() {
$info['api'] = 2;
return $info;
}
/*****************************************************************************
* INCLUDES
**/
// Loads Google Apps Profile Integration
module_load_include('inc', 'drappsprofiles');
(...)
here's drappsprofiles.views.inc
/**
*
* Implementation of hook_views_handlers().
*
**/
function drappsprofiles_views_handlers() {
return array(
'handlers' => array(
'drappsprofiles_handler_field_birthday' => array(
'parent' => 'views_handler_field',
)
)
);
}
/**
* Implementation of hook_views_data().
*
* #return array
**/
function drappsprofiles_views_data() {
(...)
$data['drappsprofiles']['birthday'] = array(
'title' => t('Birthday'),
'help' => t('Users birthday'),
'field' => array(
'handler' => 'drappsprofiles_handler_field_birthday',
'click sortable' => FALSE,
),
);
return $data;
}
drappsprofiles_handler_field_birthday.inc
<?php
/**
*
* Custom views handler for Birthday
*
*/
class drappsprofiles_handler_field_birthday extends views_handler_field {
function render($values) {
$val = unserialize($values->{$this->field_alias});
return ($val);
}
}
It seems that drappsprofiles_handler_field_birthday.inc is not being read, although I can't figure out why.
Any help would be appreciated. (I've been around this for 2 weeks!)
Assuming your (...) in .views.inc conceals code like:
$data['drappsprofiles']['table']['group'] = t('drappsprofiles');
$data['drappsprofiles']['table']['base'] = array(
'field' => 'birthday',
);
$data['drappsprofiles']['table']['join'] = array(
'#global' => array(),
);
Which I am assuming it does since your field has a group from which to select it so that it can look for the missing handler..
Then the next thing to look at is still in .views.inc in module_views_handlers() { :
return array(
++ 'info' => array(
++ 'path' => drupal_get_path('module','drappsprofilse'),
++ ),
'handlers' => array(
And beyond that, I hate to say it, but uninstalling and reinstalling the module apparently refreshes recent code tweaks to the .views.inc .. I know I had to a bunch of times, you probably noticed this too.
Related
I've got problem with validating checkbox in WooCommerce custom checkout field. I've seen this but it doesn't really help me. Custom field is generated inside form, so It should work good. I don't know if there is a need to put something more than my code in this particular case... I've tried few more hooks, but It doesn't made any effect.
add_action('woocommerce_review_order_before_submit', 'my_required_checkout_field');
function my_required_checkout_field( ) {
woocommerce_form_field( 'przetwarzanie_danych_do_zamowienia', array(
'type' => 'checkbox',
'class' => array('input-checkbox'),
'label' => __('REQUIRED ONE.'),
'required' => true,
), WC()->checkout->get_value( 'przetwarzanie_danych_do_zamowienia' ));
}
add_action('woocommerce_checkout_process', 'my_custom_checkout_field_process');
function my_custom_checkout_field_process() {
global $woocommerce;
}
/**
* Update the order meta with field value
**/
add_action('woocommerce_checkout_update_order_meta', 'my_custom_checkout_field_update_order_meta');
function my_custom_checkout_field_update_order_meta( $order_id ) {
if ($_POST['przetwarzanie_danych_do_zamowienia']) update_post_meta( $order_id, 'Oświadczenie o zapoznaniu się z regulaminem', esc_attr($_POST['przetwarzanie_danych_do_zamowienia']));
}
you are not adding the condition when your check box is not checked you have empty function my_custom_checkout_field_process so here is full working code:
add_action('woocommerce_review_order_before_submit', 'my_required_checkout_field');
function my_required_checkout_field()
{
woocommerce_form_field('przetwarzanie_danych_do_zamowienia', array(
'type' => 'checkbox',
'class' => array('input-checkbox'),
'label' => __('REQUIRED ONE.'),
'required' => true,
), WC()->checkout->get_value('przetwarzanie_danych_do_zamowienia'));
}
/**
* Process the checkout
**/
add_action('woocommerce_checkout_process', 'my_custom_checkout_field_process');
function my_custom_checkout_field_process()
{
// Check if set, if its not set add an error.
if (!$_POST['przetwarzanie_danych_do_zamowienia']) {
wc_add_notice(__('Please select required box'), 'error');
}
}
/**
* Update the order meta with field value
**/
add_action('woocommerce_checkout_update_order_meta', 'my_custom_checkout_field_update_order_meta');
function my_custom_checkout_field_update_order_meta($order_id)
{
if ($_POST['przetwarzanie_danych_do_zamowienia']) {
update_post_meta($order_id, 'Oświadczenie o zapoznaniu się z regulaminem', esc_attr($_POST['przetwarzanie_danych_do_zamowienia']));
}
}
I have created a custom field for a View using a module. To better visualise here, I have simplified it: The custom field simply generates a random number between 1 and 10.
I want to "Sort by" this random number. However I receive the following error when using this setting in Views:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'my_custom_field' in 'field list'
I am struggling to locate the error in my code.
Thanks for any assistance you can provide in my module code!!
Here are my files:
my_custom_module.info
name = My Custom Module
description = Implement random number in views.
core = 7.x
files[] = includes/views_handler_my_custom_field.inc
my_custom_module.module
<?php
/**
* Implements hook_views_api().
*/
function my_custom_module_views_api() {
return array(
'api' => 3,
);
}
my_custom_module.views.inc
<?php
/**
* Implements hook_views_data().
*/
function my_custom_module_views_data() {
$data['my_custom_module']['table']['group'] = t('My custom module');
$data['my_custom_module']['table']['join'] = array(
// Exist in all views.
'#global' => array(),
);
$data['my_custom_module']['my_custom_field'] = array(
'title' => t('My custom field'),
'help' => t('My custom field displays a random number.'),
'field' => array(
'handler' => 'views_handler_my_custom_field',
'click sortable' => TRUE,
),
'sort' => array(
'handler' => 'views_handler_sort',
),
'filter' => array(
'handler' => 'views_handler_filter_numeric',
),
);
return $data;
}
views_handler_my_custom_field.inc
<?php
/**
* #file
* Custom views handler definition.
*/
/**
* Custom handler class.
*
* #ingroup views_field_handlers
*/
class views_handler_my_custom_field extends views_handler_field {
/**
* {#inheritdoc}
*
* Perform any database or cache data retrieval here. In this example there is
* none.
*/
function query() {
}
/**
* {#inheritdoc}
*
* Modify any end user views settings here. Debug $options to view the field
* settings you can change.
*/
function option_definition() {
$options = parent::option_definition();
return $options;
}
/**
* {#inheritdoc}
*
* Make changes to the field settings form seen by the end user when adding
* your field.
*/
function options_form(&$form, &$form_state) {
parent::options_form($form, $form_state);
}
/**
* Render the random field.
*/
public function render($values) {
$random = rand(1, 10);
return $random;
}
}
Short answer: you cannot sort a view without an appropriate database field.
A bit longer answer: hook_views_data()'s main purpose is to describe a database table to Views. You did display a field which does not really exist in the database using '#global' => array(), but due the fact that that particular field was not part of the SQL query, you simply cannot sort by it. Even the value of random number you got in views_handler_my_custom_field->render() method was generated after views produced and executed SQL query, in a moment when all the sorting already took place.
I use Sonata Admin Bundle in my Symfony project and created an ArticleAdmin class for my Article entity.
In the list page, I added some custom actions to quickly publish, unpublish, delete & preview each article.
What I want to do is to hide publish button when an article is already published & vice versa.
To do this, I need to have access to each object in method configureListFields(). I would do something like this:
protected function configureListFields(ListMapper $listMapper)
{
$listMapper->add('title');
// ...
/** #var Article article */
$article = $this->getSubject();
// Actions for all items.
$actions = array(
'delete' => array(),
'preview' => array(
'template' => 'AppBundle:ArticleAdmin:list__action_preview.html.twig',
),
);
// Manage actions depending on article's status.
if ($article->isPublished()) {
$actions['draft']['template'] = 'AppBundle:ArticleAdmin:list__action_draft.html.twig';
} else {
$actions['publish']['template'] = 'AppBundle:ArticleAdmin:list__action_preview.html.twig';
}
$listMapper->add('_actions', null, array('actions' => $actions));
}
But $this->getSubjet() always returns NULL. I also tried $listMapper->getAdmin()->getSubject() and many other getters but always the same result.
What am I doing wrong ?
Thanks for reading & have a good day :)
You can do the check directly in the _action template, as you can access the current subject.
protected function configureListFields(ListMapper $listMapper)
{
$listMapper
->add('title')
->add('_action', 'actions', array(
'actions' => array(
'delete' => array(),
'preview' => array(
'template' => 'AppBundle:ArticleAdmin:list__action_preview.html.twig',
),
'draft' => array(
'template' => 'AppBundle:ArticleAdmin:list__action_draft.html.twig',
),
'publish' => array(
'template' => 'AppBundle:ArticleAdmin:list__action_publish.html.twig',
),
))
;
}
And for example in AppBundle:ArticleAdmin:list__action_draft.html.twig, you can check your condition :
{% if object.isPublished %}
your html code
{% endif %}
Something appears to have changed in the new version of WooCommerce, this snippet to change the order of the items in a formatted address used to work fine....
add_filter( 'woocommerce_order_formatted_billing_address' , 'woo_custom_order_formatted_billing_address' );
/**
* woo_custom_order_formatted_billing_address
*
* #access public
* #since 1.0
* #return void
*/
function woo_custom_order_formatted_billing_address() {
$address = array(
'first_name' => $this->billing_first_name,
'last_name' => $this->billing_last_name,
'company' => $this->billing_company,
'address_1' => $this->billing_address_1,
'address_2' => $this->billing_address_2,
'city' => $this->billing_city,
'state' => $this->billing_state,
'postcode' => $this->billing_postcode,
'country' => $this->billing_country
);
return $address;
}
But now it returns the following error...
Fatal error: Using $this when not in object context in
Can anyone point me in the right direction of either what is going wrong or an alternative way of achieving it?
I'm seeing this example on the internet, but I don't know if it ever worked. First of because in 'woocommerce_order_formatted_billing_address' filter, that is applied in class-wc-order.php file, two arguments are provided, $address ARRAY, and a reference to current WC_Order OBJECT. But your definition of the filter function does not provide any arguments. Second, error that you receive describes the problem very accurately, pseudo-variable $this is available from within an object context, but your global function is not part of any object.
Enough with technicalities, the definition should look like this:
add_filter( 'woocommerce_order_formatted_billing_address' , 'woo_custom_order_formatted_billing_address', 10, 2 );
function woo_custom_order_formatted_billing_address( $address, $wc_order ) {
// make the changes to $address array here
// use for example, $wc_order->billing_first_name, instead of $this->billing_first_name
return $address;
}
#Eolis' answer doens't work for me, because Woocommerce applies WC()->countries->get_formatted_address( $address ) and this functions erases my new fields added to array on add_filter, so my solution is add field to previous field (first_name):
add_filter('woocommerce_order_formatted_billing_address', 'my_order_formatted_billing_address', 10, 2);
function my_order_formatted_billing_address($address, $wc_order) {
$billing_last_name_2 = get_post_meta( $wc_order->id, '_billing_last_name_2', true );
$address = array(
'postcode' => $wc_order->billing_postcode,
'last_name' => $wc_order->billing_last_name,
//\n force break line
'first_name' => $wc_order->billing_first_name . "\n" . $billing_last_name_2,
'address_1' => $wc_order->billing_address_1,
'address_2' => $wc_order->billing_address_2,
'city' => $wc_order->billing_city,
'state' => $wc_order->billing_state,
'country' => $wc_order->billing_country
);
return $address;
}
I'm working with a drupal 6.26 install. I can see file names like page-2011-custom-landing-page.tpl.php in the theme directory I'm using.
From what I understand, I should be able to see this template at http://www.mydomain.com/2011-custom-landing-page however I just get a 'page not found' message at that address. What's going on?
If you see a file name as 'page-2011-custom-landing-page.tpl.php' in you theme folder it means that there is a template file named 'page-2011-custom-landing-page.tpl.php' using for a page. That page may be defined in one of your custom module.
Like this:
<?php
/**
* Implements hook_menu().
*/
function custommodulename_menu() {
$items['pathname'] = array(
'title' => 'title',
'page callback' => 'custommodulename_pagename',
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
);
return $items;
}
/**
* Implements hook_theme().
* Adds our theme specificiations to the Theme Registry.
*/
function custommodulename_theme($existing, $type, $theme, $path) {
$items = array();
$items['custommodulename_pagename_page'] = array(
'render element' => 'form',
'template' => 'page-2011-custom-landing-page', //name of file(template) to be created,here create page-2011-custom-landing-page.tpl.php in the custom module folder
);
return $items;
}
/**
* Callback function(menu)
*/
function custommodulename_pagename(){
return theme('custommodulename_pagename_page');
}
?>
page-2011-custom-landing-page is not a url, it is a template name. You can see the content inside the template my accessing the menu callback that using that template. (here it is : http://yoursite.com/pathname)
Reference : http://www.developerdoc.com/answer/add-template-menu-call-back