I'm trying to create a new custom field formatter for text fields but nothing is showing at all.
I tried un-installing and re-installing the module and flushed the cache many times, and nothing worked.
Here's the code I used
/**
* Implementation of hook_field_formatter_info()
*/
function facebooklink_field_formatter_info()
{
return array(
'cfieldformatter_text_1' => array(
'label' => t('Text 1'),
'field types' => array('text', 'text_long', 'text_with_summary'),
'settings' => array(
'pic_size' => 'small',
'tooltip' => 'Link to user Facebook page',
),
),
);
}
/**
* Implementation of hook_field_formatter_settings_form()
*/
function facebooklink_field_formatter_settings_form($field, $instance, $view_mode, $form, &$form_state)
{
//This gets the view_mode where our settings are stored
$display = $instance['display'][$view_mode];
//This gets the actual settings
$settings = $display['settings'];
//Initialize the element variable
$element = array();
$element['pic_size'] = array(
'#type' => 'select', // Use a select box widget
'#title' => t('Button Size'), // Widget label
'#description' => t('Select what size of FB button'), // Helper text
'#default_value' => $settings['pic_size'], // Get the value if it's already been set
'#options' => array(
'small' => 'Small',
'medium' => 'Medium',
'large' => 'Large',
),
);
$element['tooltip'] = array(
'#type' => 'textfield', // Use a textbox
'#title' => t('Tool Tip'), // Widget label
'#description' => t('This text will appear when a user mouses over.'), // helper text
'#default_value' => $settings['tooltip'], // Get the value if it's already been set
);
return $element;
}
/**
* Implementation of hook_field_formatter_settings_summary()
*/
function facebooklink_field_formatter_settings_summary($field, $instance, $view_mode)
{
$display = $instance['display'][$view_mode];
$settings = $display['settings'];
$summary = t('Use a #size Facebook button with the tooltip of "#tooltip"', array(
'#size' => $settings['pic_size'],
'#tooltip' => $settings['tooltip'],
));
return $summary;
}
/**
* Implementation of hook_field_formatter_view()
*/
function facebooklink_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display)
{
$element = array(); // Initialize the var
$settings = $display['settings']; // get the settings
$size = $settings['pic_size']; // The Size setting selected in the settings form
$tooltip = $settings['tooltip']; // The tool tip assigned in settings
// Create the image - Note that I'm storing the images in our module but they could be anywhere
$image = '<img src="/' . drupal_get_path('module', 'facebooklink') . 'fb-' . $size . '.png">';
foreach ($items as $delta => $item) {
$fb = $item['safe_value']; // Getting the actual value
}
$options = array(
'html' => TRUE, // This tells Drupal that we're sending HTML, not plain text, otherwise it would encode it
'attributes' => array(
'title' => $tooltip, // This sets our tooltip
),
);
if(isset($fb)) {
$link = l($image, $fb, $options); // Create the Link
$element[0]['#markup'] = $link; // Assign it to the #markup of the element
}
return $element;
}
Any help with this crazy issue?!!!.
I would suggest you either name your formatter 'facebooklink' instead 'cfieldformatter_text_1' in hook_field_formatter_info (this works when you have one single formatter in a module) or explicity trigger the formatter in hook_field_formatter_view as below:
/**
* Implementation of hook_field_formatter_view()
*/
function facebooklink_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display)
{
$element = array(); // Initialize the var
$settings = $display['settings']; // get the settings
switch ($display['type']) {
case 'cfieldformatter_text_1':
$size = $settings['pic_size']; // The Size setting selected in the settings form
$tooltip = $settings['tooltip']; // The tool tip assigned in settings
// Create the image - Note that I'm storing the images in our module but they could be anywhere
$image = '<img src="/' . drupal_get_path('module', 'facebooklink') . 'fb-' . $size . '.png">';
foreach ($items as $delta => $item) {
$fb = $item['safe_value']; // Getting the actual value
}
$options = array(
'html' => TRUE, // This tells Drupal that we're sending HTML, not plain text, otherwise it would encode it
'attributes' => array(
'title' => $tooltip, // This sets our tooltip
),
);
if(isset($fb)) {
$link = l($image, $fb, $options); // Create the Link
$element[0]['#markup'] = $link; // Assign it to the #markup of the element
}
break;
}
return $element;
}
Related
I want to add a new attribute to an existing woocommerce product. I use this code
$pf = new WC_Product_Factory();
$product = $pf->get_product(76);
//Create the attribute object
$attribute = new WC_Product_Attribute();
$attribute->set_id( 0 );
//pa_color slug
$attribute->set_name( 'color' );
//Set terms slugs
$attribute->set_options( array(
'blue',
) );
$attribute->set_position( 0 );
//If enabled
$attribute->set_visible( 1 );
$product->set_attributes(array($attribute));
$id = $product->save();
this code working fine but I want to add attributes with terms, this code create custom product attribute.
I want to create attribute like this image
How can I create key-value attributes?
Try this code
function get_attribute_id_from_name($name)
{
global $wpdb;
$attribute_id = $wpdb->get_var("SELECT attribute_id
FROM {$wpdb->prefix}woocommerce_attribute_taxonomies
WHERE attribute_name LIKE '$name'");
return $attribute_id;
}
function save_product_attribute_from_name($name, $label = '', $set = true)
{
if (!function_exists('get_attribute_id_from_name')){
return;
}
global $wpdb;
$label = $label == '' ? ucfirst($name) : $label;
$attribute_id = get_attribute_id_from_name($name);
$taxonomy = wc_attribute_taxonomy_name($name); // The taxonomy slug
if (empty($attribute_id)) {
$attribute_id = NULL;
}
else {
$set = false;
}
//register existing taxonomy
if (isset($attribute_id) && !taxonomy_exists($taxonomy)) {
register_attribute($name);
}
$args = array(
'attribute_id' => $attribute_id,
'attribute_name' => $name,
'attribute_label' => $label,
'attribute_type' => 'select',
'attribute_orderby' => 'menu_order',
'attribute_public' => 0,
);
if (empty($attribute_id)) {
$wpdb->insert("{$wpdb->prefix}woocommerce_attribute_taxonomies", $args);
set_transient('wc_attribute_taxonomies', false);
}
if ($set) {
$attributes = wc_get_attribute_taxonomies();
$args['attribute_id'] = get_attribute_id_from_name($name);
$attributes[] = (object)$args;
//print_r($attributes);
set_transient('wc_attribute_taxonomies', $attributes);
} else {
return;
}
}
function register_attribute($name)
{
$taxonomy = wc_attribute_taxonomy_name($name); // The taxonomy slug
$attr_label = ucfirst($name); // attribute label name
$attr_name = (wc_sanitize_taxonomy_name($name)); // attribute slug
register_taxonomy(
$taxonomy,
'product',
array(
'label' => __($attr_label),
'rewrite' => array('slug' => $attr_name),
'hierarchical' => true,
)
);
}
function add_product_attribute($product_id, $data)
{
$_pf = new WC_Product_Factory();
$product = $_pf->get_product($product_id);
$product_attributes = array();
foreach ($data['_attributes'] as $key => $terms) {
$taxonomy = wc_attribute_taxonomy_name($key); // The taxonomy slug
$attr_label = ucfirst($key); // attribute label name
$attr_name = (wc_sanitize_taxonomy_name($key)); // attribute slug
// NEW Attributes: Register and save them
if (!taxonomy_exists($taxonomy))
save_product_attribute_from_name($attr_name, $attr_label);
$product_attributes[$taxonomy] = array(
'name' => $taxonomy,
'value' => '',
'position' => '',
'is_visible' => 1,
'is_variation' => 1,
'is_taxonomy' => 1
);
foreach ($terms as $value) {
$term_name = ucfirst($value);
$term_slug = sanitize_title($value);
// Check if the Term name exist and if not we create it.
if (!term_exists($value, $taxonomy))
wp_insert_term($term_name, $taxonomy, array('slug' => $term_slug)); // Create the term
// Set attribute values
wp_set_object_terms($product_id, $term_name, $taxonomy, true);
}
$product->set_attributes(array($attribute));
}
update_post_meta($product_id, '_product_attributes', $product_attributes);
}
Then call function
add_product_attribute(76, [
'attributes' => [
'Attribute 1' => ['Value 1', 'Value 2'],
'Attribute 2' => ['Value 4', 'Value 5'],
],
]
);
I created a custom endpoint for specific data from a custom table in my Wordpress plugin. It get's all the data from the table with the getHelpers() function. After that it will be merged by some user data. I would like to add the profile_image as a link to the response so we can get it with the embed parameter.
What is the best way to add the link to the response? I know the $response->add_link() function but this would add it to the response and not to each contributor.
I tried to add the link as an array but this won't react on the _embed parameter.
This is my code for the custom endpoint:
class VEMS_Rest_Contributors extends WP_REST_Controller {
protected $namespace = 'vems/v2';
protected $rest_base = 'contributors';
/**
* Register the routes for coupons.
*/
public function register_routes() {
register_rest_route( $this->namespace, '/' . $this->rest_base, array(
'methods' => WP_REST_Server::READABLE,
'callback' => array( $this, 'get_items' ),
'args' => $this->get_collection_params(),
) );
}
public function get_items( WP_REST_Request $request ) {
$project_id = $request->get_param( 'project_id' );
$contributors = array();
if( !empty($project_id) ) {
$project = new VEMS_Project( $request['project_id'] );
$helpers = $project->getHelpers();
foreach($helpers as $helper) {
$contributor = array();
if( !empty($helper->contributor_id) ) {
$user = get_user_by( 'ID', $helper->contributor_id );
$user_meta = get_user_meta( $helper->contributor_id );
$contributor['ID'] = $helper->contributor_id;
$contributor['user_nicename'] = $user->data->display_name;
$contributor['user_profile_image'] = $user_meta['contributor_profile_image'][0];
} else {
$contributor['user_nicename'] = $helper->name;
$contributor['user_profile_image'] = $helper->image_id;
}
$contributor['item_total'] = $helper->item_total;
$contributor['checked'] = $helper->checked;
$contributor['helper_date'] = $helper->helper_date;
/*
$contributor['_links']['profile_image'] = array(
'href' => rest_url( '/wp/v2/media/' . $contributor['user_profile_image'] ),
'embeddable' => true
);
*/
$contributors[] = $contributor;
}
}
$response = rest_ensure_response( $contributors );
return $response;
}
public function get_collection_params() {
$params['project_id'] = array(
'description' => __( 'Limit result set to contributors assigned a specific project.', 'vems' ),
'type' => 'integer',
'sanitize_callback' => 'absint',
'validate_callback' => 'rest_validate_request_arg',
);
return $params;
}
}
to handle links on route vems/v2/contributors?_embed, the element profile_image must be an array of links and then you can do that
$contributor['_links']['profile_image'] = [
[
'href' => rest_url( '/wp/v2/media/' . $contributor['ID'] ),
'embeddable' => true,
],
];
I am building a custom module which should display a calendar created by a third-part library. So far, so good. The only problem is, the calendar appears on top of page, even above header. It does not seems to be css-related.
This is the way it looks:
This is my module file:
require_once 'includes/apphp-calendar/calendar.class.php';
function calendario_menu(){
$items = array();
$items['eventos/calendario'] = array(
'title' => 'Calendario',
'description' => 'Calendario de Eventos.',
'page callback' => '_page_calendario',
'access callback' => TRUE,
'type' => MENU_CALLBACK,
);
return $items;
}
function _page_calendario() {
$objCalendar = new Calendar();
$calendar = $objCalendar->Show();
return array(
'#markup' => $calendar
);
}
Seems to me like the Show function of the calendar class outputs rather than returning a string.
try this:
function _page_calendario() {
$objCalendar = new Calendar();
ob_start();
$objCalendar->Show();
$calendar = ob_get_contents();
ob_end_clean();
return array(
'#markup' => $calendar
);
}
I am adding some autocomplete on a form alter. The problem is that in the callback, only the string in the textfield The autocomplete is on, is available. I also want to access a value from another textfield in the callback. How is this possible ?
/**
* Implements hook_form_alter().
*/
function webform_conversion_jquery_form_webform_client_form_1_alter(&$form, &$form_state, $form_id) {
//Load some extra function to process data
module_load_include('inc', 'webform_conversion_jquery', '/includes/dataqueries');
//Add extra js files
drupal_add_js(drupal_get_path('module', 'webform_conversion_jquery') . '/js/conversionform.js');
$form['submitted']['correspondentadress']['cor_street']['#autocomplete_path'] = 'conversionform/conversion_street';
}
}
/**
* Implements hook_menu().
*/
function webform_conversion_jquery_menu() {
$items = array();
$items['conversionform/conversion_street'] = array(
'title' => 'Conversion street autocomplete',
'page callback' => 'conversion_street_autocomplete',
'access callback' => 'user_access',
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
);
return $items;
}
/**
* Retrieve a JSON object containing autocomplete suggestions for streets depending on the zipcode.
*/
function conversion_street_autocomplete($street = '') {
$street = "%" . $street . "%";
$matches = array();
$result = db_select('conversion_adresslist')
->fields('conversion_adresslist', array('street'))
->condition('street', $street, 'like')
->execute();
foreach ($result as $street) {
$matches[$street->street] = $street->street;
}
drupal_json_output($matches);
}
I just want to be able to post extra information in the function:
conversion_street_autocomplete($street = '', $extraparameter)
I had the same problem and have figured out a way, which is not too strenuous. It involves overriding the textfield theme and then passing your parameter to the theme function.
First create declare your theme function:
function mymodule_theme() {
$theme_hooks = array(
'my_module_autocomplete' => array(
'render element' => 'element',
),
);
return $theme_hooks;
}
Next we need to add the theme and the variable to our form element. In my case, the form element is part of a field widget:
function my_module_field_widget_form($form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
if($instance['widget']['type'] == 'my_module_field_type') {
$element['my_module_field'] = array(
'#type' => 'textfield',
'#autocomplete_path' => 'my-module/autocomplete',
// THIS IS THE IMPORTANT PART - ADD THE THEME AND THE VARIABLE.
'#theme' => 'my_module_autocomplete',
'#my_module_variable' => $field['field_name'],
);
}
return $element;
}
Then implement the theme function. This is a copy of theme_textfield from includes/form.inc with one important difference - we append the variable to the autocomplete path:
function theme_my_module_autocomplet($variables) {
$element = $variables['element'];
$element['#attributes']['type'] = 'text';
element_set_attributes($element, array('id', 'name', 'value', 'size', 'maxlength'));
_form_set_class($element, array('form-text'));
$extra = '';
if ($element['#autocomplete_path'] && drupal_valid_path($element['#autocomplete_path'])) {
drupal_add_library('system', 'drupal.autocomplete');
$element['#attributes']['class'][] = 'form-autocomplete';
$attributes = array();
$attributes['type'] = 'hidden';
$attributes['id'] = $element['#attributes']['id'] . '-autocomplete';
// THIS IS THE IMPORTANT PART. APPEND YOUR VARIABLE TO THE AUTOCOMPLETE PATH.
$attributes['value'] = url($element['#autocomplete_path'] . '/' . $element['#my_module_variable'], array('absolute' => TRUE));
$attributes['disabled'] = 'disabled';
$attributes['class'][] = 'autocomplete';
$extra = '<input' . drupal_attributes($attributes) . ' />';
}
$output = '<input' . drupal_attributes($element['#attributes']) . ' />';
return $output . $extra;
}
Now the variable will be available as the first parameter on the autocomplete callback function:
function _my_module_autocomplete($my_module_variable, $search_string) {
// Happy days, we now have access to our parameter.
}
Just in case anyone is still having trouble with this I found a great solution while trying to figure out how to do this. I had a year select list and that dictated what data was displayed in the autocomplete field. The solution basically has an ajax callback function for the select list that can then update the autocomplete field with an extra parameter in the url. Anyways, it is really well explained in the following article.
http://complexdan.com/passing-custom-arguments-drupal-7-autocomplete/
*A note of caution, I was going crazy trying to figure out why it did not work and it turns out you can't have the same form on the page twice (I needed to because I was displaying it differently for mobile devices) because you are using an id for the ajax callback. I added an extra argument to accomplish that. It is called uniqueid in the below example.
function report_cards_comparison_form($form, &$form_state, $uniqueid) {
$curryear = t('2012');
$form['year_select'] = array(
'#title' => t('School Year'),
'#type' => 'select',
'#options' => array(
'2012' => t('2012'),
'2013' => t('2013'),
'2014' => t('2014'),
'2015' => t('2015'),
),
'#default_value' => $curryear,
'#ajax' => array(
'callback' => 'report_cards_comparison_form_callback',
'wrapper' => $uniqueid,
'progress' => array(
'message' => 'Updating Schools...',
'type' => 'throbber'
),
),
);
$form['choice'] = array(
//'#title' => t('Search By: School Name'),
'#type' => 'textfield',
'#attributes' => array(
'class' => array('school-choice'),
'placeholder' => t('Start Typing School Name...'),
),
'#required' => TRUE,
'#autocomplete_path' => 'reportcards/autocomplete/' . $curryear,
'#prefix' => '<div id="' . $uniqueid . '">',
'#suffix' => '</div>',
);
$form['submit'] = array(
'#type' => 'submit',
'#prefix' => '<div class="submit-btn-wrap">',
'#suffix' => '</div>',
'#value' => t('Search'),
'#attributes' => array('id' => 'add-school-submit'),
);
return $form;
}
/**
* Ajax Callback that updates the autocomplete ajax when there is a change in the Year Select List
*/
function report_cards_comparison_form_callback($form, &$form_state) {
unset($form_state['input']['choice'], $form_state['values']['choice']);
$curryear = $form_state['values']['year_select'];
$form_state['input']['choice'] = '';
$form['choice']['#value'] = '';
$form['choice']['#autocomplete_path'] = 'reportcards/autocomplete/' . $curryear;
return form_builder($form['#id'], $form['choice'], $form_state);
}
and I can call the form by doing this...
print render(drupal_get_form('report_cards_comparison_form', 'desktop-schoolmatches'));
You can do it by overriding methods from autocomplete.js in your own js. Here is example:
(function($) {
Drupal.behaviors.someModuleOverrideAC = {
attach: function(context, settings) {
// Next is copied and adjusted method from autocomplete.js
Drupal.jsAC.prototype.populatePopup = function() {
var $input = $(this.input);
var position = $input.position();
// Show popup.
if (this.popup) {
$(this.popup).remove();
}
this.selected = false;
this.popup = $('<div id="autocomplete"></div>')[0];
this.popup.owner = this;
$(this.popup).css({
top: parseInt(position.top + this.input.offsetHeight, 10) + 'px',
left: parseInt(position.left, 10) + 'px',
width: $input.innerWidth() + 'px',
display: 'none'
});
$input.before(this.popup);
// Do search.
this.db.owner = this;
if ($input.attr('name') === 'field_appartment_complex') {
// Overriden search
// Build custom search string for apartments autocomplete
var $wrapper = $('div.apartments-autocomplete');
var $elements = $('input, select', $wrapper);
var searchElements = {string: this.input.value};
$elements.each(function() {
searchElements[$(this).data('address-part')] = $(this).val();
});
var string = encodeURIComponent(JSON.stringify(searchElements));
this.db.search(string);
}
else {
// Default search
this.db.search(this.input.value);
}
};
}
};
}(jQuery));
In your server callback:
function some_module_autocomplete_ajax($string) {
// Decode custom string obtained using overriden autocomplete js.
$components = drupal_json_decode(rawurldecode($string));
// Do you search here using multiple params from $components
}
Ok, for as far as I can see it is not possible. maybe you can roll your own with the ajax functionality in fapi http://api.drupal.org/api/drupal/developer--topics--forms_api_reference.html/7#ajax
For now I solved it by implementing jquery.ui.autocomplete which is included in drupal 7
My main question is:
Does the theme_hook() function gets called whenever the form gets rebuilt via ahah (ahah_helper) ?
I'm trying to show a select box, with some filtering options, when the user changes it, the table below it changes too.
I have this by now:
function veiculos_listar_form($form_state)
{
$form = array();
ahah_helper_register($form, $form_state);
//biulds $options
$form['listar_veics'] = array(
'#type' => 'fieldset',
'#prefix' => '<div id="listar-veics-wrapper">',
'#suffix' => '</div>',
'#tree' => TRUE,
);
if (!isset($form_state['values']['listar_veics']['filial']))
$form['#filial_veic'] = 1;
else
$form['#filial_veic'] = $form_state['values']['listar_veics']['filial'];
$form['listar_veics']['filial'] = array(
'#type' => 'select',
'#title' => "Listar veículos da filial",
'#options' => $filiais,
'#default_value' => $form['#filial_veic'],
'#ahah' => array(
'event' => 'change',
'path' => ahah_helper_path(array('listar_veics')),
'wrapper' => 'listar-veics-wrapper',
'method' => 'replace',
),
);
return $form;
}
function veiculos_listar_form_submit($form, &$form_state)
{
}
function _listar_veiculos_tabela($filial)
{
//builds $header and $data
$table = theme_table($header, $data);
return $table;
}
function theme_veiculos_listar_form($form)
{
$output = drupal_render($form);
$filial = $form['#filial_veic'];
$output .= '<br>' . $filial . '<br>';
$output .= _listar_veiculos_tabela($filial);
return $output;
}
function veiculos_theme() {
return array(
'veiculos_listar_form' => array(
'arguments' => array('form' => NULL),),
);
}
In my little and innocent world, it should work if theme_hook is called on every ahah event (change).
The problem is, the variable printed is always the same, like what the user is choosing isn't being stored. If the user select a different options, it shows the new option, but the $filial variable is always the same when the theme prints.
Like this:
http://img230.imageshack.us/img230/9646/62144334.jpg
Any suggestion on what i could do to make this work? I'm developing our own module, so using views module isn't a good idea.
Thanks.
You should redo code this way.
Ahah callback I do not wrote, I think you would not have problem with it.
Check some examples on drupal.org
function veiculos_listar_form($form_state)
{
$form = array();
ahah_helper_register($form, $form_state);
//biulds $options
// remove divs because we do not want to reload selector with ahah
$form['listar_veics'] = array(
'#type' => 'fieldset',
'#tree' => TRUE,
);
if (!isset($form_state['values']['listar_veics']['filial']))
$form['#filial_veic'] = 1;
else
$form['#filial_veic'] = $form_state['values']['listar_veics']['filial'];
// add cover div here, because we will reload table
$form['table'] = array(
'#prefix' => '<div id="listar-veics-wrapper">',
'#suffix' => '</div>',
'#type' => 'markup',
'#value' => _listar_veiculos_tabela($form['#filial_veic']),
);
$form['listar_veics']['filial'] = array(
'#type' => 'select',
'#title' => "Listar veículos da filial",
'#options' => $filiais,
'#default_value' => $form['#filial_veic'],
'#ahah' => array(
'event' => 'change',
'path' => ahah_helper_path(array('listar_veics')),
'wrapper' => 'listar-veics-wrapper',
'method' => 'replace',
),
);
return $form;
}
function veiculos_listar_form_submit($form, &$form_state)
{
}
function _listar_veiculos_tabela($filial)
{
//builds $header and $data
$table = theme_table($header, $data);
return $table;
}
function theme_veiculos_listar_form($form)
{
$output = drupal_render($form);
return $output;
}
function veiculos_theme() {
return array(
'veiculos_listar_form' => array(
'arguments' => array('form' => NULL),),
);
}