Concrete5 - how to get page_list edit screen to read values in custom fields - concrete5

1. Setup
We've added two layout dropdown fields to the page_list block's edit screen by overriding:
db.xml
page_list_form.php
Adding the fields to the view
2. In db.xml we've added:
<field name="gridSize" type="C" size="255">
</field>
<field name="gridPaddingStyle" type="C" size="255">
</field>
3. In page_list_form.php
We've added the slect fields to the block edit screen like this:
<div class="ccm-block-field-group">
<h2><? echo t('Grid layout')?></h2>
<p><? echo t('Grid size')?></p>
<?php
$gridSize = array(
'1up' => 'grid-list-item-single grid-unit-1',
'2up' => 'grid-unit-2',
'3up' => 'grid-unit-3',
'4up' => 'grid-unit-4',
'5up' => 'grid-unit-5',
'6up' => 'grid-unit-6'
);
if (is_array($gridSize)) :
?>
<select name="gridSize" id="selectGridSize">
<? foreach ($gridSize as $gridItem => $value) : ?>
<option value="<?= $value ?>" <?php if ($gridSize == $value) { ?> selected <?php } ?>>
<?= $gridItem ?>
</option>
<? endforeach; ?>
</select>
<? endif; ?>
<p><? echo t('Grid padding style')?></p>
<?php
$gridPaddingStyle = array(
'Padding' => '',
'No padding' => 'grid-no-padding',
'Hairline' => 'grid-hairline'
);
if (is_array($gridPaddingStyle)) :
?>
<select name="gridPaddingStyle" id="selectPaddingSize">
<? foreach ($gridPaddingStyle as $gridPaddingStyleItem => $value) : ?>
<option value="<?= $value ?>" <?php if ($gridPaddingStyle == $value) { ?> selected <?php } ?>>
<?= $gridPaddingStyleItem ?>
</option>
<? endforeach; ?>
</select>
<? endif; ?>
</div>
4. In the view.php we've added:
$gridSize = $controller->gridSize;
$gridPadding = $controller->gridPaddingStyle;
Which obviously pulls the data out of the database for use when we output the markup.
Everything works great except when we come to re-edit the block - essentially the values previously set for our custom fields don't get read and the dropdowns revert back to the first items in the select lists.
5. Question
How do we get the page_list edit screen to read the values previously set in the database?
Any pointers in the right direction would be much appreciated (sorry, can't work out how to get syntax highlighting working - wish the markdown was the same ad Github issues).
Cheers
Ben

Your problem here is that you're overwriting the variables in your edit view $gridSize = array(...) just before you're trying to access them, you can probably fix it by changing those array variable names to something semantic like "grid_size_options".
If that doesn't work, you can use $this->set to pass variables to the view from the controller edit method. Your edit method would look something like this:
public function edit()
{
$this->set('grid_size', $this->gridSize);
$this->set('grid_padding_style', $this->gridPaddingStyle);
}
and then your edit view can just magically access $grid_size and $grid_padding_style.
<select name="gridPaddingStyle" id="selectPaddingSize">
<?php
foreach ($gridPaddingStyle as $gridPaddingStyleItem => $value) {
?>
<option value="<?= $value ?>" <?= $grid_padding_style == $value ? 'selected' : '' ?> >
<?= $gridPaddingStyleItem ?>
</option>
<?php
}
?>
</select>

Related

Gravity Form on WordPress

I am trying to pull dynamic form id to create a dropdown for the end-user. This I have done here.
Now, I'd like to pass that id so that a different form shows up based on selection as shown below:
<form class="ui form">
<select name="select" class="ui fluid search selection dropdown">
<?php $myforms = RGFormsModel::get_forms(); foreach ($myforms as $form) { ?>
<option name="select" value="<?php echo $form->id ?>"> <?php echo $form->title ?> </option>
<?php } ?>
</select>
</form>
<?php echo gravity_form($form, true, true, false, '', true, 1); ?>
Here's what I might do to make a shortcode for gravity forms that pulls the ID from the URL parameter.
function return_gform_embed(){
$form_id = $_GET["form_id"];
return do_shortcode('[gravityform id="'.$form_id.'" title="false" description="false"]');
}
add_shortcode('form-url', 'return_gform_embed');
You'd add the shortcode [form-url] to the page where you'd like the form to display.
So now the URL with ?form_id=6 would load the gravity form with id 6. I didn't test the whole javascript aspect but assuming you can change the URL and force a reload things should work out ok.
Here is what I ended up doing if it helps someone else out there:
<form class="ui form">
<select name="select" id="select" class="ui fluid search selection dropdown" onchange="location = this.options[this.selectedIndex].value;">
<?php
function return_gform_embed(){
$form_id = $_GET["form_id"];
return do_shortcode('[gravityform id="'. $form_id .'" title="false" description="false"]');
}
add_shortcode('form-url', 'return_gform_embed');
?>
<?php $myforms = RGFormsModel::get_forms(); foreach ($myforms as $form) { ?>
<option value="/prevention/sessions/?form_id=<?php echo $form->id ?>"> <?php echo $form->title ?> </option>
<?php } ?>
</select>
</form>
<?php echo do_shortcode('[form-url]') ?>

Get all values of repeater subfields stored in DB

Currently, I'm struggling with ACF (advanced custom fields) repeater field. The first issue is the general architecture of this field type - it's not stored in the database in a single place, yet its multiplied (e.g. if my repeater field name is called workshops_grouping and subfield is called data, then there will be workshops_grouping_1_data, workshops_grouping_2_data etc.).
So I wanted to be smarter than ACF ( :) ) and I've thought that if I will use below code to get all post ids from CPT, then it will be easier. Here is the code:
<?php
$other_page = get_posts( array(
'posts_per_page' => -1,
'fields' => 'ids',
'post_type' => 'workshops',
'post_status' => 'publish',
) );
if( have_rows('workshops_grouping', $other_page) ): ?>
<select id="no1">
<?php while( have_rows('workshops_grouping', $other_page) ): the_row(); ?>
<option value="<?php the_sub_field('data'); ?>"><?php the_sub_field('data'); ?></option>
<?php endwhile; ?>
</select>
<select id="no2">
<?php while( have_rows('workshops_grouping', $other_page) ): the_row(); ?>
<option value="<?php the_sub_field('company'); ?>"><?php the_sub_field('company'); ?></option>
<?php endwhile; ?>
</select>
<?php endif; ?>
But hey, guess what - it doesn't work at all. Meaning = it shows nothing. I've gone through a couple of similar posts here as well on the official community board of ACF. So far without success. Is it even doable?
According to the ACF documentation, you have to pass an ID inside the have_rows function.
https://www.advancedcustomfields.com/resources/have_rows/
Now your passing the $other_page variable inside the function. This variable is an array with your pages. You need to loop this first and pass the post ID in de have_rows function.
your code will look like something similar to this:
<?php
$other_page = get_posts( array(
'posts_per_page' => -1,
'fields' => 'ids',
'post_type' => 'workshops',
'post_status' => 'publish',
) );
foreach ( $other_page as $page ) {
if( have_rows('workshops_grouping', $page->ID) ): ?>
<select id="no1">
<?php while( have_rows('workshops_grouping', $page->ID) ): the_row(); ?>
<option value="<?php the_sub_field('data'); ?>"><?php the_sub_field('data'); ?></option>
<?php endwhile; ?>
</select>
<select id="no2">
<?php while( have_rows('workshops_grouping', $page->ID) ): the_row(); ?>
<option value="<?php the_sub_field('company'); ?>"><?php the_sub_field('company'); ?></option>
<?php endwhile; ?>
</select>
<?php endif;
}
Make sure that the variable $other_page has some pages and the ACF field workshops_grouping has some rows.

How to add a text before the sorting option in woocommerce

I've been looking around the net but all they have is to teach you how to add/edit/remove an option in the sorting dropdown. But my template requires me to add a 'Sort By' text. Like this:
I tried adding it via Jquery but it keeps being gone after ajax filters.
Please don't rank this down, there might be someone who forgot that there is a format file for this in themes/woocomerce/loop/orderby.php
Then just edit the code like this:
<form class="woocommerce-ordering" method="get">
<span class="sort-name">Sort By</span>
<select name="orderby" class="orderby">
<?php foreach ( $catalog_orderby_options as $id => $name ) : ?>
<option value="<?php echo esc_attr( $id ); ?>" <?php selected( $orderby, $id ); ?>>
<?php echo esc_html( $name ); ?>
</option>
<?php endforeach; ?>
</select>
<input type="hidden" name="paged" value="1" />
<?php wc_query_string_form_fields( null, array( 'orderby', 'submit', 'paged', 'product-page' ) ); ?>

Custom posts Drop down menu problem - wordpress

Im try this code to get my custom posts list in drop down in sidebar.
<form action="<? bloginfo('url'); ?>" method="get">
<select name="page_id" id="page_id">
<?php
global $post;
$args = array( 'post_type'=>'name',
'numberposts' => -1
);
$posts = get_posts($args);
foreach( $posts as $post ) : setup_postdata($post); ?>
<option value="<? echo $post->ID; ?>"><?php the_title(); ?></option>
<?php endforeach; ?>
</select>
<input type="submit" name="submit" value="view" />
</form>
Every thing nice code get the list of my custom posts and also show in drop down. but the visit button not work in my code. when i pick name from drop down and click on visit its just redirect to home page not to post. please help me.
One More thing can we make this like that if user pick name from drop down menu user automatic redirect to that post, I mean user dont need to click on visit button. anyways both answers welcome. thanks in advance.(sorry for bad English)
Please Try this
<select name="page_id" id="page_id" onChange="this.options[this.selectedIndex].value && (window.location = this.options[this.selectedIndex].value);">
<?php
global $post;
$args = array( 'post_type'=>'name',
'numberposts' => -1
);
$posts = get_posts($args);
foreach( $posts as $post ) : setup_postdata($post); ?>
<option value="<? echo get_the_permalink($post->ID); ?>"><?php the_title(); ?></option>
<?php endforeach; ?>
</select>
You can take help of javaScript.
Below illustration will help you to get the desired result.
When the user selects the option, the page automatically will be redirected to the post.
<select onchange="javascript:location.href = this.value;">
<?php
foreach ($posts as $post) {
setup_postdata($post);
echo '<option value="'.the_permalink().'" >'.the_title().' </option>';
}
?>
</select>

Filter Categories in Wordpress framework

I am working on a project where I have to create two dropdown menus and populate them with categories; subcategories actually.
I have two main categories and I want to populate the menus with their subcategories comparing the parents.
<select>
<option data-filter="*"><?php echo __('All', 'Avada'); ?></a></option>
<?php foreach($portfolio_taxs as $portfolio_tax_slug => $portfolio_tax_name): ?>
<?php if($portfolio_category = 'taste') ?>
<?php { ?>
<option data-filter=".<?php echo $portfolio_tax_slug; ?>" ><?php echo $portfolio_tax_name; ?>
<?php } ?>
</option>
<?php endforeach; ?>
</select>
Nothing seems to be working.
Help please!
Actually I managed to do what I wanted. The solution was to get the terms of the taxonomy:
$ideas_terms = get_terms('portfolio_category', array
( 'orderby' => 'count',
'hide_empty=> 0,
'parent'=>13
)
);
This returns an array with all element with
parentID == 13
Then to call the other elements of the array you have to use:
<?php foreach($ideas_terms as $term): ?>
<option data-filter=".<?php echo $term->slug; ?>" href="#">
<?php echo $term->name; ?>
</option>
<?php endforeach; ?>

Resources