How to display dropdown list from related entities in Symfony? - symfony

I want to create a drop-down list from two related entities "Category" and "sub-category".
Under symfony I know how to create it from a single entity at a time but I have not managed to do so in a way that in the drop-down list each category displays the subcategories that belongs to it as indicated in the figure bellow.

You have to give arrays to your 'choices' field, each one representing a category.
$builder->add('stockStatus', ChoiceType::class, array(
'choices' => array(
'Main Statuses' => array(
'Yes' => 'stock_yes',
'No' => 'stock_no',
),
'Out of Stock Statuses' => array(
'Backordered' => 'stock_backordered',
'Discontinued' => 'stock_discontinued',
),
),
));
you can find more information here: https://symfony.com/doc/current/reference/forms/types/choice.html#grouping-options

Related

How to filter WP_Query results by the custom field of a related post?

I have two post types:
Venues
Reviews
The Venues Post Type contains the following ACF custom fields:
Region
Sub Region
The Reviews Post Type contains one ACF custom field:
Venue (which is Post Object - Select Field)
I need to display all Reviews who's Venue is in a specific region and/or Sub Region.
Is this something that can be accomplished using WP_Query? Or do I need to do a fancy database call?
This is what I thought would initially work but it seems that you can not get the custom field of a post object as a meta_query..
$args = array(
'post_type' => 'review',
'posts_per_page' => 18,
'paged' => $paged,
'meta_key' => 'venue',
'meta_query' => array(
array(
'key' => 'region',
'value' => 'napa-valley'
)
)
);
I think you need 2 loops here, first loop through the venues using region meta query (you could just use get_posts() or get_pages() instead of WP_Query too)
e.g.
'meta_query' => array(
array(
'key' => 'region',
'value' => 'napa-valley'
)
)
Then you can push the IDs of the venues in specific regions into an array
array_push($venue_ids, $post->ID);
Then you can use the $venue_ids array in your second loop which would loop through the review using a meta query to match the venues from your first loop ids to the post object ids selected in the review page.
'meta_query' => array(
array(
'key' => 'venue',
'value' => $venue_ids
)
)
Let me know if this is helpful and if you think this will work for you and I can offer further assistance if I haven't explained correctly or you need help.

how to show 3 fields in one row in WooCommerce checkout page

I am doing customization of WooCommerce but I want to show 3 fields in one row I have tried a lot but no success. I have pasted the image of current fields
screenshot
I want to show send amount, receive amount and provider in one row
this is the code about the custom field
woocommerce_form_field('receive', array(
'type' => 'text',
'required' =>true,
'class' => array(
'woocommerce-input-wrapper form-row form-row-first validate-required'
) ,
'label' => __('Receive Amount') ,
'placeholder' => __('Receive Amount') ,
) ,
$checkout->get_value('receive'));

Symfony2 form collection with custom labels

I have an object - Book with Persons collection inside (each person has name property) and I need to create a form type persons it is a collection simply checkbox but I would like to have as a label on each checkbox persons name how to do it ?
I have BookType:
$builder->add('persons', 'collection', array(
'type' => 'checkbox',
'options' => array(
'required' => false,
),
));
How to add label with persons name ??
I'm not sure if I understood your question, but I'll try to help you anyway.
Did you already tried this?
$builder->add('persons','entity', array(
'class' => 'VendorBundle:Entity',
'property' => 'name',
'label' => 'YourLabel'
))
(Adapt the code for your configurations by changing 'VendorBundle:Entity' and 'YourLabel' )
This adds a field in your form which has the 'property name' of each 'VendorBundle:Person' and has a label 'YourLabel' right before the dropdown field.

How to add two sonata_type_collection fields of the same property in the same admin?

Suppose you have ArticleAdmin and CommentsAdmin. It is easy to add one-to-many editing in Sonata:
$formMapper->add('comments', 'sonata_type_collection',
array(
'by_reference' => false,
),
array(
'edit' => 'inline',
'inline' => 'table',
)
);
However, suppose I have more complicated CommentsAdmin form and it can has two visualizations depending on the kind of comment in it. I would like to display two sonata_type_collection fields in ArticleAdmin for grouping different types of comments into two different edit tables.
Adding another add('comments', ...) is of course incorrect, and adding add('comments2', ...) results in exception.
I will manage separating comments between two fields in admin, but how to create sonata_type_collection field on a virtual entity field Article::comments2? How to tell Sonata Admin what kind of collection should it be?
I'm not sure if this will help you but:
->add('categoryHasMedia', 'sonata_type_collection', array(
'cascade_validation' => true,
'label' => 'Logo\'s'
), array(
'edit' => 'inline',
'inline' => 'table',
'link_parameters' => array('context' => $context),
'admin_code' => 'appstrakt.project.admin.category_has_media',
))
By using admin_code you can tell which admin class you want to use for that sonata_type_collection if I'm not mistaken.

Allow user to dynamically add fields and values to a node in Drupal

I'm trying to come up with a way for a user to populate a form with only the fields needed for a given piece of content.
So for example I have a content type called "Research Project" and I want to add a series of measurements to each one.
eg.
Proj 1:
Water Sampling
Measurements:
Gallons of water collected - 20 gal
Ambient temperature at time of collection - 75 deg F
PPM lead found in water - 2 ppm
The measurements criteria would be entered into the system ahead of time and they would select it from a dropdown. I imagine the UI would look like this for the form:
select measurement from dropdown > Field to enter value
+ link to add unlimited new measurement and value rows
The measurements are variable from project to project, there's a lot of them and there's always new ones being added making it impractical to add every potential measurement as it's own CCK field. There's also too many different types of project to make content types for each project practical. So essentially I'm looking for the ability to build the form and populate results on the fly. How should I go about doing this? I could input my measurements as taxonomy terms, but then how could I assign and store a value for each?
there is two functions
field_create_field() can create field a itself.
field_create_instance() - binds a field to the bundle.
Example from this post:
$field = array(
'field_name' => 'field_' . $vocabulary->machine_name,
'type' => 'taxonomy_term_reference',
'cardinality' => FIELD_CARDINALITY_UNLIMITED,
'settings' => array(
'allowed_values' => array(
array(
'vocabulary' => $vocabulary->machine_name,
'parent' => 0,
),
),
),
);
field_create_field($field);
$instance = array(
'field_name' => 'field_' . $vocabulary->machine_name,
'entity_type' => 'node',
'label' => 'Tags',
'bundle' => 'article',
'description' => $vocabulary->help,
'widget' => array(
'type' => 'taxonomy_autocomplete',
'weight' => -4,
),
'display' => array(
'default' => array(
'type' => 'taxonomy_term_reference_link',
'weight' => 10,
),
'teaser' => array(
'type' => 'taxonomy_term_reference_link',
'weight' => 10,
),
),
);
field_create_instance($instance);

Resources