Change field using theme_preprocess_field() - drupal

gunwinner is my theme name and i want to change a field on particular content type.
I have tried this code,
function gunwinner_preprocess_field(&$variables) {
$element = $variables['element'];
if($element['#field_name'] == 'field_deal_url') {
$values = $element['#items'][0]['value'];
$deal_link = l(t("Go to Store"), "$values", array('attributes' => array('class' => 'store-link')));
$element['#items'][0]['value'] = "Store";
return;
}
}
which returns me the array with the link title "Go to Store" but it doesn't reflect on the page for the particular field.
Can anyone help me out for this?

You're making a copy of $variables['element'] and manipulating that, so the changes aren't being persisted into the original variable.
Just change the first line of your function to
$element = &$variables['element'];

Related

ACF default field group settings for "hide on screen"

I would like to set default settings for style and hide on screen settings within the field group settings.
I've found posts talking about modifying settings for individual ACF fields like the WYSIWYG or image fields like so.
add_filter( 'acf/get_valid_field', 'change_post_content_type');
function change_post_content_type( $field ) {
if($field['type'] == 'wysiwyg') {
$field['tabs'] = 'visual';
$field['media_upload'] = 0;
}
if($field['type'] == 'image') {
$field['preview_size'] = 'small';
}
if($field['type'] == 'style') {
$field['style'] = 'seamless';
}
return $field;
}
Using this overrides whatever is selected rather than setting the value as default but it's good enough for what I need.
The image and WYSIWG field work fine but I can't get it working on the field group setting fields. I don't think the $field['type'] == 'style' is correct but as it doesn't follow the same structure as the other fields I don't know what I should be using.
Any ideas?
update
I've found this but I can't figure out how to use it. The following doesn't work
add_action('acf/render_field_group_settings', 'change_field_group_settings', 10, 1);
function change_field_group_settings( $field_group ) {
$field_group['style'] = 'seemless';
$field_group['hide_on_screen'] = array('the_content');
return $field_group;
}
You may need change your syntax to use proper formatting for style and hide_on_screen, for example:
'hide_on_screen' => array(
0 => 'the_content',
),
Best practice would be to create a new Field Group in your WP CMS, publish it, and navigate to:
Custom Fields > Tools > (check the box for your Field Group) > Generate PHP
That way you can view the proper output.

WordPress Shortcode with fallback

I'm building a simple plugin that uses the geoservices web service and what I'm trying to do is dynamically change the content on a WordPress page based on their location. I have it working somewhat but my issue is that it's returning both the location-specific text AND the default. I know it's because i'm using the shortcode instance more than once but I don't know how to change it to ONLY show the location specific content and if the location is not set or does not match the shortcode params then fall back to the default one. I don't want to add "default" as a shortcode param because it could contain HTML or something else.
Here is an example of my shortcode:
[geo city="Orlando"]555-123-6349[/geo][geo city="Raleigh"]919-999-9999[/geo][geo city="Default"]Default text here[/geo]
So based on the above, the desired result would show Orlando's phone number if the user is from Orlando or it would show Raleigh number if they are from Raleigh. Otherwise, if they are not from either of those places, it would use the default.
Here is my shortcode:
function geo_services( $atts , $content = null ) {
// Attributes
extract(shortcode_atts(array(
'city' => '',
'state' => '',
), $atts));
require_once('geoplugin.class.php');
$geoplugin = new geoPlugin();
$geoplugin->locate();
if($city === $geoplugin->city){
return $content;
} elseif ($state === $geoplugin->region){
return $content;
} elseif ($city === 'Default') {
return $content;
}
}
add_shortcode( 'geo', 'geo_services' );
And here is what is happening when I use the example shortcode above:
I believe you may be misunderstanding how shortcodes work in WP. In your example, you have added 3 shortcodes to the content. Each of those shortcodes is going to run. Not one or the other. So doing,
[geo city="Orlando"]555-123-6349[/geo][geo city="Raleigh"]919-999-9999[/geo][geo city="Default"]Default text here[/geo]
means that each of those will be called and evaluated. $geoplugin->city is always going to return the city of the user, regardless of what attributes you supplied. And since you are returning $content in all cases, it will always spit out the content that you added inside the shortcode. This is why you are seeing all 3 responses.
Instead, I would try the approach below. If your goal is to spit out content based on the city of the user, you really don't need to supply an attribute to the shortcode. See the following example:
//in your post/page content, simply use the shortcode geo
[geo]
//your function should be
function geo_services( $atts , $content = null ) {
//
require_once('geoplugin.class.php');
//
$geoplugin = new geoPlugin();
$geoplugin->locate();
//
switch( $geoplugin->city ) {
case 'Orlando':
return '555-123-6349';
break;
case 'Raleigh':
return '919-999-9999';
break;
default:
return 'Default text here';
break;
}
}
add_shortcode( 'geo', 'geo_services' );
Providing another answer based on OP comments. If you really need to manage the content via the WYSIWYG, then you could supply the content for each city as an attribute.
//add shortcode to post/page content
[geo orlando="555-123-6349" raleigh="919-999-9999" default="Custom default text here"]
//your function should be
function geo_services( $atts , $content = null ) {
//don't use extract since we expect X number of atts now
$atts = shortcode_atts(array(
'default' => 'Default text here'
), $atts);
//
require_once('geoplugin.class.php');
//
$geoplugin = new geoPlugin();
$geoplugin->locate();
//was the city provided as an attribute?
if( isset($atts[ strtolower($geoplugin->city) ]) ) {
return $atts[ strtolower($geoplugin->city) ];
}else {
return $atts['default'];
}
}
add_shortcode( 'geo', 'geo_services' );
You may have to get creative with the HTML portion of the content, but now you can include X number of cities, with their custom content, in the shortcode itself. If the city is not supplied, or does not match, it will fallback to the default.

Change field value and submit node with a custom button

I am attempting to write a module that adds a button to a node type that, when pressed, will change a value of a field in that node and submit the changes. Everything seems to be working, as the button appears correctly and the node submits when it's pressed, but the value of the field remains unchanged. I feel like I'm missing something obvious.
<?php
function iu_buttons_node_view($node, $view_mode, $langcode) {
if ($node->type == 'billing_entry') {
if ($node->field_status['und'][0]['value'] == 'open') {
$form = drupal_get_form('submit_button_form');
$node->content['submit_button'] = $form;
}
}
}
function submit_button_form($form, &$form_submit) {
$form['submit'] = array(
'#type' => 'button',
'#value' => ('Submit'),
'#submit' => array('submit_button_form_submit'),
);
return $form;
}
function submit_button_form_submit($form, &$form_state) {
$node->field_status['und'][0]['value']['#value'] = 'submitted';
}
It's probably worth noting that the field I'm trying to change is a select list. Should I be using a different function than hook_form_submit?
I am assuming you are writing this code in a custom module named iu_buttons and you want to display this button in the node page, not in the node edit form.
In this case, the problem is that you never saved the node in your submit function. Here is a version of your submit function that will save the node.
function submit_button_form_submit($form, &$form_state) {
$node = menu_get_object(); //get the current node
$node->field_status['und'][0]['value']['#value'] = 'submitted';
node_save($node); // save the changed node
}
I think you may be interested in saving only the field, without the need to save the whole node. In this case, you might use the following:
function submit_button_form_submit($form, &$form_state){
$node = new stdClass(); // Create empty object
$node->nid = intval(args(1)); // Include the nid so that Drupal saves the new value to the correct node
$node->field_status['und'][0]['value']['#value'] = 'submitted';
field_attach_update('node', $node); // Save the field
}

Add custom icon to orders' list view in Prestashop

I would like to add new button to order's list view, but I have no idea how to do it:
I want to do it in way that new upgrade will not delete it.
Edit: This new button will just open new browser window so it's completely independent of PrestaShop functionalities. But I would like to put it in this toolbar line.
Thank you for your help!
You could do it with overrides. Create a file called AdminOrdersController.php in your overrides/controllers/admin/ folder, and add the following:
<?php
class AdminOrdersController extends AdminOrdersControllerCore
{
public function initPageHeaderToolbar()
{
parent::initPageHeaderToolbar(); // this will assign native icons
// This is where you add you custom icon
$this->page_header_toolbar_btn['my_custom_icon'] = array(
'href' => self::$currentIndex.'&mycustomaction&token='.$this->token,
'desc' => $this->l('My custom action', null, null, false),
'icon' => 'process-icon-save'
);
}
public function initProcess()
{
parent::initProcess();
if (Tools::getIsset('mycustomaction')) {
if ($this->tabAccess['view'] === '1') {
$this->display = 'mycustomaction';
$this->action = 'mycustomaction';
}
else
$this->errors[] = Tools::displayError('You do not have permission to edit this.');
}
}
public function initContent()
{
parent::initContent();
if ($this->display == 'mycustomaction')
$this->content.= $this->renderMyCustomAction();
}
public function renderMyCustomAction()
{
// this is where you render your custom page.
}
}
Note that this is a quick mock up. It should work, though :)
UPDATE
If you just want the icon to open a new page, leave only the initPageHeaderToolbar method and provide the right href attribute, you can delete the initProcess, initContent and renderMyCustomAction methods. I'll leave them in my original reply in case someone else finds it useful.

How to use nodeload in Drupal 7 to print referred node field?

I am in a node, I created a field using "References" module to relate one content type to another. Now... The 2 content type are "PRACTISE" (a node with title, description ecc...) and "TECHNOLOGY", a node with just logo images. I want to show related logo into node--practise.tpl.php. How can i do this in DP7?
I wouldn't do it directly in the template file, instead you'd be better off implementing hook_preprocess_node in your theme's template.php file to pass the logo(s) in as a variable. The logic is the same either way:
function mytheme_preprocess_node(&$vars) {
$node = $vars['node'];
if ($node->type == 'practise') {
$related_node_nid = $node->field_related_field_name['und'][0]['nid'];
$related_node = node_load($related_node_nid);
$logos = '';
foreach ($related_node->field_logo_field_name['und'] as $img) {
$logos .= theme('image', array('path' => $img['uri'], 'alt' => 'Alt text'));
}
$vars['related_logos'] = $logos;
}
}
Then in your template.php file you will have the variable $logos which will contain the list of logos you built up in the preprocess function. Obviously you can tailer the HTML to suit your needs, and you need to swap in the correct field names for field_related_field_name and field_logo_field_name.

Resources