Wordpress Oxygen ACF repeater empty condition - wordpress

I have a gallery as an ACF repeater in wordpress.
In Oxygen builder there is a Condition functionality which allows to check some basic stuff, like if some field is blank, if the fields contains something, operators like "==", "!=" etc.
But for repeaters they don't work.
I haven't found a built-in solution to check if a repeater fields is empty or not.
I want to hide the section which holds my gallery if there are no images in the gallery.

Here's the solution:
I have created a custom condition via the Conditions API in Oxygen.
I added this code snippet via the Code Snippets plugin:
if( function_exists('oxygen_vsb_register_condition') ) {
oxygen_vsb_register_condition('Repeater empty checker', array('options'=>array(), 'custom'=>true), array('is not empty', 'is empty'), 'check_if_repeater_is_empty', 'Other');
function check_if_repeater_is_empty($value, $operator) {
if ($operator == "is not empty") {
return have_rows($value, get_the_ID());
} else if ($operator == 'is empty') {
return !have_rows($value, get_the_ID());
}
}
}
And now I have a "Repeater empty checker" in the condition dropdown.
As a value I just enter the name of the fields e.g. gallery and it checks if the gallery is empty or not.

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.

possible woocommce bug keep showing "total_sales" in metabox "custom field" add/edit product page?

As shown in the edit/add product page, this item "total_sales" always show up in the default metabox "custom field".
It's not doing any harm yet but it's annoying.
However, someone might edit the total sales number by accident and would cause problems.
I am writing a child theme from storeFront.
Is this a bug in woocommerce or did I accidentally changed something somewhere that causes this?
This is by default. If you think this is a bug then you can always open a thread on https://github.com/woocommerce/woocommerce/issues
EDIT
Custom fields or post meta entries can be hidden from the by default in Wordpress available »Custom Fields«-Metabox by prefixing them with a underscore - _ - as noted here.
If you want to hide the field you can use this piece of code that i wrote for you
function filter_is_protected_meta( $protected, $meta_key, $meta_type ) {
if ( $meta_key == 'total_sales' ) {
$protected = true;
}
return $protected;
}
add_filter( 'is_protected_meta', 'filter_is_protected_meta', 10, 3 );

How can I hide/show advanced custom fields based on selection of a select field?

I created some advanced custom fields. One of them is a select field, where you can choose between sale and charter.
I want to hide/show some of my advanced custom fields based on what is currently selected (sale or charter).
How can I do this?
ACf Image
As shown in image (Click link) there is an option in the ACF to show fields conditionally based on other fields value whether it is Checkbox or Select box.
The other answer covers the back end. For the front end template, something like this:
if (get_field('select_field_name') == 'sale'){
//show some stuff i.e. the_field('field_name');
}
elseif (get_field('select_field_name') == 'charter') {
//show other fields
}
else {
//maybe do something here
}

Drupal 8 - Ignore contextual filter

On a search view, linked to search api and facets, I want to add contextual filters on my content type.
This content type have referenced entity fields, linked to taxonomy term (one field to taxonomy from destination vocabulary, the other to taxonomy from activity vocabulary)
So, I have created 2 contexual filter, one for each "taxonomy field".
view
filter
But, I appears only the first filter (destination) is applied. If my taxonomy term from URL is a destination, the view displays right results. But if it's an activity, it display all contents. So I supposed there is a problem with contextual filter validator : 'Action to take if filter value does not validate' should have something like 'Ignore filter' option, because with 'Display all results for the specified field', its shows everything, but don't execute next filter.
Anyone has a solution ?
Thanks a lot
Finally, I found a solution, just altering view in pre_build
/**
* Implements hook_views_pre_build().
*/
function my_module_views_pre_build(ViewExecutable $view)
{
if ($view->id() == 'tour_search' && $view->current_display == 'tours_taxonomy') {
$tid = reset($view->args);
if (! $tid) {
return;
}
/** #var Term $term */
$term = Term::load($tid);
if ($term->getVocabularyId() === 'activities') {
unset($view->argument['field_tour_destination']);
return;
}
if ($term->getVocabularyId() === 'destinations') {
unset($view->argument['field_tour_activity']);
return;
}
return;
}
}
I moved validation logic in this hook, by unsetting filter in with a simple term vocabulary test.
Hope it will helps somebody !

How can I get the title of a form element in Drupal?

For example, in the registration form, there is "Username" and the text field for it which has the input type="text" name="name" ....
I need to know how can I get the title from the input field's name.
I'm expecting a function like:
$title = get_title_for_element('name');
Result:
assert($title == 'Username'); // is true
Is there something like this in Drupal?
Thanks.
You have the form and the form state variables available to your validation function. You should use form_set_error() to set the error.
There is no function that I am aware of which will map from the values array to the form array. But it is not dificult to work it out. Understanding the form data structure is one of the key skills you need when building drupal.
In this case the form in question is generated (in a roundabout way) by user_edit_form, you can see the data structure in there.
$form['account']['name'] is the username field. and the array key for the title is '#title' as it will be in most cases for form elements.
You can do it in two different ways as I see it. Let's create a module called mycustomvalidation.module (remember to create the mycustomvalidation.info file also).
Note: The code below has not been tested, so you might have to do some minor adjustments. This is Drupal 6.x code by the way.
1) Using hook_user()
What you need is a custom module containing your own implementation of hook_user() http://api.drupal.org/api/function/hook_user/6.
<?php
function mycustomvalidation_user($op, &$edit, &$account, $category = NULL) {
if ($op == 'validate') {
// Checking for an empty 'profile_fullname' field here, but you should adjust it to your needs.
if ($edit['profile_fullname'] != '') {
form_set_error('profile_fullname', t("Field 'Fullname' must not be empty."));
}
}
}
?>
2) Using form_alter() and a custom validation function
Personally, I would go for this option because I find it cleaner and more "correct". We're adding a custom validation function to our profile field here.
<?php
function mycustomvalidation_form_alter(&$form, $form_state, $form_id) {
// Check if we are loading 'user_register' or 'user_edit' forms.
if ($form_id == 'user_register' || $form_id == 'user_edit') {
// Add a custom validation function to the element.
$form['User information']['profile_fullname']['#element_validate'] = array('mycustomvalidation_profile_fullname_validate');
}
}
function mycustomvalidation_profile_fullname_validate($field) {
// Checking for an empty 'profile_fullname' field here, but you should adjust it to your needs.
if ($field['#value'] != '') {
form_set_error('profile_fullname', t("Field %title must not be empty.", array('%title' => $field['#title']));
}
}
?>

Resources