How can I filter through my meta query via acf fields? - meta-query

I have a group of posts that can select a custom field 'Partner Content'. These posts then select their post author. The post author is a custom post type that has a relationship field that selects the 'Partner Company' associated with the post author.
I am now trying to set up a single-partner_company.php template that displays all posts associated with the specific company. My meta query looks like this:
<?php
$current_company = $post;
$comp_id = $current_company->ID;
$comp_name = $current_company->post_title;
?>
<?php
$current_company = $post;
$comp_id = $current_company->ID;
$comp_name = $current_company->post_title;
?>
<?php
$args = array(
'numberposts' => -1,
'fields' => 'ids',
'post_type' => 'partner_authors',
'meta_query' => array(
'key' => 'author_company',
'value' => $current_company
)
);
$authors = get_posts( $args );
$post_args = array(
'fields' => 'ids',
'numberposts' => 3,
'post_type' => 'post',
'meta_query' => array(
'key' => 'post_author',
'value' => $authors
)
);
$partner_posts = get_posts( $post_args );
$count = 0;
?>
neither array is working for me. what am I doing wrong?

Related

How to load post ID using the gform_form_post_get_meta filter?

Trying to load post ID (and then some ACF field) of the post, where the form is currently embedded. Using get_the_id() or global $post w/ $post->ID returns NULL.
Loading post ID works correctly when using the other Gravity Forms filters (e.g. gform_admin_pre_render), but I was told using the gform_form_post_get_meta is the better way to do ths. What is the right approach for this?
add_filter( 'gform_form_post_get_meta' , 'my_populate_cpt_as_choices' );
function my_populate_cpt_as_choices( $form ) {
$current_post_id = get_the_id();
$postargs = array(
'post_type' => 'suhlasy',
'post_status' => 'publish',
'posts_per_page' => '-1',
);
$posts = get_posts( $postargs );
$input_id = 1; // this makes sure the checkbox labels and inputs correspond
foreach ( $posts as $post ) {
//skipping index that are multiples of 10 (multiples of 10 create problems as the input IDs)
if ( $input_id % 10 == 0 ) {
$input_id++;
}
$post_id = $post->ID;
$id_souhlasu = 1200 + $input_id;
$title = get_the_title($post_id);
$checkbox_text = get_field('checkbox_text', $post_id);
$text_suhlasu = get_field('text_suhlasu', $post_id);
$kategoria = get_field('kategoria_suhlas', $post_id);
// getting other fields for this post to display as values or checkbox labels
$nazev_souhlasu = GF_Fields::create( array(
'type' => 'consent',
'id' => $id_souhlasu, // The Field ID must be unique on the form
'formId' => $form['id'],
'isRequired' => true,
'label' => $title,
'parameterName' => 'my_custom_parameter',
'checkboxLabel' => $checkbox_text,
'description' => '<h2>' . $current_post_id . $post_id . $kategoria . '</h2><br>' . $text_suhlasu,
'pageNumber' => 1, // Ensure this is correct
) );
$form['fields'][] = $nazev_souhlasu;
$input_id++;
}
return $form;
}

Yii2 autocomplete no response

Am trying to display users list as mail.Here it is my code,but there is no response
<?php
$data = Users::find()
->select(['user_email as value', 'user_id as id'])
->asArray()
->all();
echo AutoComplete::widget([
'name' => 'user_email',
'id' => 'ddd',
'clientOptions' => [
'source' => $data,
'autoFill'=>true,
'minLength'=>'1',
'select' => new JsExpression("function( event, ui ) {
$('#user_mail_1').val(ui.item.id);
}")],
]);
?>
<?= $form->field($model, 'user_email')->HiddenInput(['id' => 'user_mail_1'])->label(false) ?>
You need to use label for display value of auto complete input value.
So, get user_email as label like as:
<?php
use yii\web\JsExpression;
$data = Users::find()
->select(['user_email as value', 'user_id as id', 'user_email as label'])
->asArray()
->all();
// OR try below query for get data.
$data = (new \yii\db\Query())
->select(["user_email as value", "user_email as label","user_id as id"])
->from('users u')
->all();
echo AutoComplete::widget([
'name' => 'user_email',
'id' => 'ddd',
'clientOptions' => [
'source' => $data,
'autoFill'=>true,
'minLength'=>'1',
'select' => new JsExpression("function( event, ui ) {
$('#".Html::getInputId($model, 'user_email')."').val(ui.item.id); // Html::gtInputId() get dynamic id of input field.
}")],
]);
?>
<?= $form->field($model, 'user_email')->hiddenInput()->label(false) ?>

Drupal 7 | Form managed file upload image preview

I'm can't get to work image preview on Drupal 7 Form managed file.
I have for code like this in template.php:
function testform($form, &$form_state) {
$form = array();
$form['con_image'] = array(
'#type' => 'managed_file',
'#title' => t('Image'),
'#required' => TRUE,
'#default_value' => variable_get('con_image', ''),
'#progress_indicator' => 'bar',
'#progress_message' => 'Uploading ...',
'#upload_location' => 'public://gallery/',
'#theme' => 'test',
'#upload_validators' => array(
'file_validate_is_image' => array(),
'file_validate_extensions' => array('jpg jpeg'),
'file_validate_image_resolution' => array('6000x4000','800x600'),
'file_validate_size' => array(6 * 1024 * 1024),
),
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Add to site'),
);
return $form;
}
I call testform after checking additional conditions (like userpoints) via code
$arr = drupal_get_form('testform');
print drupal_render($arr);
The form itself is working, I'm able to add image to node (programmatically) but cannot get the preview of image during upload process. I try to use #theme but it seems doesn't work at all.
My theme function looks like this:
function theme_test($variables) {
$element = $variables['element'];
$output = '';
$base = drupal_render_children($element); // renders element as usual
if($element['fid']['#value'] != 0) {
// if image is uploaded show its thumbnail to the output HTML
$output .= '<div class="multifield-thumbnail">';
$output .= theme('image_style', array('style_name' => 'thumbnail', 'path' => file_load($element['fid']['#value'])->uri, 'getsize' => FALSE));
$output .= '</div>';
}
Any ideas?
You need to declare your theme with a hook_theme in your module.
function yourmodule_theme() {
return array(
'test' => array(
'render element' => 'element',
)
);
}

Drupal form option populate from loop

I want to populate select option from loop
$form['vote']['selected'] = array(
'#type' => 'select',
'#title' => 'Select',
'#name' => 'name',
);
foreach($loop as $row)
$form['vote']['selected']['#options'] = array($row->id => $row->name);
}
return $form;
Need some help?
This is the standard way to do it:
$options = array();
foreach($loop as $row)
$options[$row->id] = $row->name;
}
$form['vote']['selected'] = array(
'#type' => 'select',
'#title' => 'Select',
'#name' => 'name',
'#options' => $options
);
You might also look at the fetchAllKeyed method of the database query which is a handy shortcut to get data from the database into a keyed array suitable for select lists:
$options = db_query('SELECT id, name FROM {table}')->fetchAllKeyed();
The above will produce exactly the same as the foreach loop above.

Ordering in find()

I have a model Org.
In a model Org I have $hasAndBelongsToMany = array('Patient','Project','Category');
In orgs controller action view I get data from database with
$this->set('org', $this->Org->find('first',array('condition'=> array('id'=>$id))));
Result is ok but I want change the order.
I want order result Patient by name, Project by name and Category by id.
How use ORDER BY for Patient,Project,Category in find() ?
Assuming this is Cake, the ordering of associated models should be done in the model when you declare the HABTM relationship:
<?php
class Org extends AppModel {
var $hasAndBelongsToMany = array(
'Patient' => array(
'order' => array(
'Patient.name ASC'
)
),
'Project' => array(
'order' => array(
'Project.name ASC'
)
),
'Category' => array(
'order' => array(
'Category.id ASC'
)
)
);
}
?>
If you're wanting to sort the actual result set by the associated models you can pass an order array to the find method:
<?php
$org = $this->Org->find('all', array(
'conditions' => array(
'Org.column_name' => $yourVar
),
'order' => array(
'Patient.name ASC',
'Project.name ASC',
'Category.id ASC'
)
));
?>
If you're doing a find first by a unique field (like an ID) you can use the built in findByFIELDNAME helper:
<?php
$org = $this->Org->findById($id);
$this->set(compact(array('org')));