I am trying to allow customers to change their role on their WordPress account page and all works very well functionality wise except what is being shown on the front end. When they come to change their role, the option selected is always "Select an option..." rather than the actual role that they are designated to (such as Example 1). I have excluded some roles (such as Administrator) as I only want to show them certain roles. What do i change in the below code to fix this and show their current role?
function iconic_get_account_fields() {
return apply_filters( 'iconic_account_fields', array(
'first_name' => array(
'type' => 'text',
'label' => __( 'First Name', 'iconic' ),
'hide_in_account' => true,
'hide_in_admin' => true,
'hide_in_checkout' => true,
'hide_in_registration' => false,
'required' => true,
),
'last_name' => array(
'type' => 'text',
'label' => __( 'Last Name', 'iconic' ),
'hide_in_account' => true,
'hide_in_admin' => true,
'hide_in_checkout' => true,
'hide_in_registration' => false,
'required' => true,
),
'role' => array(
'type' => 'select',
'label' => __( 'Franking Machine Model', 'iconic' ),
'hide_in_account' => false,
'hide_in_admin' => true,
'hide_in_registration' => true,
'hide_in_checkout' => true,
'options' => array(
'' => __( 'Select an option...', 'iconic' ),
'example_1_customer' => __( 'Example 1', 'iconic' ),
'example_2_customer' => __( 'Example 2', 'iconic' ),
'example_3_customer' => __( 'Example 3', 'iconic' ),
'other_customer' => __( 'Other', 'iconic' ),
),
),
) );
}
Get current role of user using below function
function get_current_user_role(){
$current_user = wp_get_current_user();
$roles = $current_user->roles;
return $roles[0];
}
SOLUTION #1
Pass it to woocommerce_form_field($key, $args, $value) function , consider below example ,
$fields = iconic_get_account_fields();
$role = get_current_user_role();
foreach ($fields as $key => $field_args) {
if ($key == 'role') {
woocommerce_form_field($key, $field_args, $role);
} else {
woocommerce_form_field($key, $field_args);
}
}
SOLUTION #2
Make modification of function you shared as below , I have added 'value' argument.
function iconic_get_account_fields() {
return apply_filters('iconic_account_fields', array(
'first_name' => array(
'type' => 'text',
'label' => __('First Name', 'iconic'),
'hide_in_account' => true,
'hide_in_admin' => true,
'hide_in_checkout' => true,
'hide_in_registration' => false,
'required' => true,
),
'last_name' => array(
'type' => 'text',
'label' => __('Last Name', 'iconic'),
'hide_in_account' => true,
'hide_in_admin' => true,
'hide_in_checkout' => true,
'hide_in_registration' => false,
'required' => true,
),
'role' => array(
'type' => 'select',
'label' => __('Franking Machine Model', 'iconic'),
'hide_in_account' => false,
'hide_in_admin' => true,
'hide_in_registration' => true,
'hide_in_checkout' => true,
'options' => array(
'' => __('Select an option...', 'iconic'),
'example_1_customer' => __('Example 1', 'iconic'),
'example_2_customer' => __('Example 2', 'iconic'),
'example_3_customer' => __('Example 3', 'iconic'),
'other_customer' => __('Other', 'iconic'),
),
'value' => apply_filters('selectedrole','') // added new arg
),
));
add_filter('selectedrole', 'get_current_user_role');
Let me know which work best for you.
Related
I have created two custom post types in my wordpress project: city and property using the below query.
register_post_type('city',
array(
'labels' => array(
'name' => __('City', 'dc_qode'),
'singular_name' => __('City', 'dc_qode'),
),
'public' => true,
'show_in_menu' => true,
'rewrite' => array('slug' => 'city'),
'show_ui' => true,
'has_archive' => false,
'hierarchical' => true,
'show_tagcloud' => false,
'supports' => array(
'title',
'editor',
'thumbnail',
),
'can_export' => true,
'taxonomies' => array( 'city'),
)
);
register_post_type('property',
array(
'labels' => array(
'name' => __('Property', 'dc_qode'),
'singular_name' => __('Property', 'dc_qode'),
),
'public' => true,
'show_in_menu' => true,
'rewrite' => array('slug' => 'property'),
'show_ui' => true,
'has_archive' => false,
'hierarchical' => true,
'show_tagcloud' => false,
'supports' => array(
'title',
'editor',
'thumbnail',
),
'can_export' => true,
'taxonomies' => array( 'property'),
)
);
Using this, I can access any property using the url http://domain-name.com/property/property-name. But I want to access the url as http://domain-name.com/city-name/property-name (for eg. http://domain-name.com/toronto/abcproperty). The city-name will be assigned with each property. I tried to use the slug, 'city', within property as:
'rewrite' => array('slug' => '%city%')
in place of
'rewrite' => array('slug' => 'property')
But it's not working.
How can I achieve this scenario?
You can make your custom posts to have parents like page. So there are three main thing you have to do:
Make you custom post to support Page attributes.
You have to register new rewrite rule, which will then define a new permastruct which will be used in the third step.
After registering the new rewrite rule, you should apply this rule to your custom posts' permalinks.
Complete working code
add_action('init', function(){
$labels = array(
"name" => "City",
"singular_name" => "City",
"menu_name" => "City",
"all_items" => "All City",
"add_new" => "Add New",
"add_new_item" => "Add New City",
"edit" => "Edit",
"edit_item" => "Edit City",
"new_item" => "New City",
"view" => "View",
"view_item" => "View City",
"search_items" => "Search City",
"not_found" => "No City Found",
"not_found_in_trash" => "No City Found in Trash",
"parent" => "Parent City",
);
$args = array(
"labels" => $labels,
"description" => "",
"public" => true,
"show_ui" => true,
"has_archive" => true,
"show_in_menu" => true,
"exclude_from_search" => false,
"capability_type" => "post",
"map_meta_cap" => true,
"hierarchical" => true,
"rewrite" => array( "slug" => "city", "with_front" => true ),
"query_var" => true,
"supports" => array( "title", "revisions", "thumbnail" )
);
register_post_type( "city", $args );
$labels = array(
"name" => "Properties",
"singular_name" => "Property",
);
$args = array(
"labels" => $labels,
"description" => "",
"public" => true,
"show_ui" => true,
"has_archive" => true,
"show_in_menu" => true,
"exclude_from_search" => false,
"capability_type" => "post",
"map_meta_cap" => true,
"hierarchical" => false,
"rewrite" => array( "slug" => "city/%city_name%", "with_front" => true ),
"query_var" => true,
"supports" => array( "title", "revisions", "thumbnail" )
);
register_post_type( "properties", $args );
});
add_action('add_meta_boxes', function() {
add_meta_box('properties-parent', 'City', 'properties_attributes_meta_box', 'properties', 'side', 'default');
});
function properties_attributes_meta_box($post) {
$pages = wp_dropdown_pages(array('post_type' => 'city', 'selected' => $post->post_parent, 'name' => 'parent_id', 'show_option_none' => __('(no parent)'), 'sort_column'=> 'menu_order, post_title', 'echo' => 0));
if ( ! empty($pages) ) {
echo $pages;
} // end empty pages check
}
add_action( 'init', function() {
add_rewrite_rule( '^city/(.*)/([^/]+)/?$','index.php?properties=$matches[2]','top' );
});
add_filter( 'post_type_link', function( $link, $post ) {
if ( 'properties' == get_post_type( $post ) ) {
//Lets go to get the parent city name
if( $post->post_parent ) {
$parent = get_post( $post->post_parent );
if( !empty($parent->post_name) ) {
return str_replace( '%city_name%', $parent->post_name, $link );
}
} else {
//This seems to not work. It is intented to build pretty permalinks
//when properties has not parent, but it seems that it would need
//additional rewrite rules
//return str_replace( '/%city_name%', '', $link );
}
}
return $link;
}, 10, 2 );
I am using Elementor. In one section user can create categories with repeater field. In other section I would like to list these categories in Select2 control. How could I access category_data in _register_controls function? I know I can access it in render function.
protected function _register_controls() {
$this->start_controls_section(
'content_section',
[
'label' => __( 'Content', 'plugin-name' ),
'tab' => \Elementor\Controls_Manager::TAB_CONTENT,
]
);
$repeater->add_control(
'category_title', [
'label' => esc_html__( 'Title', 'plugin-name'),
'type' => \Elementor\Controls_Manager::TEXT,
'label_block' => false,
]
);
$repeater->add_control(
'category_slug', [
'label' => esc_html__( 'Slug', 'plugin-name'),
'type' => \Elementor\Controls_Manager::TEXT,
'label_block' => false,
]
);
$this->add_control(
'category_data',
[
'label' => esc_html__( 'Category', 'plugin-name'),
'type' => \Elementor\Controls_Manager::REPEATER,
'fields' => $repeater->get_controls(),
'prevent_empty' => false,
'title_field' => '{{{ category_title }}}',
]
);
$this->end_controls_section();
$this->start_controls_section(
'category_section',
[
'label' => __( 'Category', 'plugin-name' ),
'tab' => \Elementor\Controls_Manager::TAB_CONTENT,
]
);
//how could I access category_data here?
$settings = $this->get_settings_for_display();
$category_data = $settings['category_data'];
$cd_arr = [];
foreach ($category_data as $cd) {
$cd_arr[] = $cd['category_slug'];
}
if(count($cd_arr) > 0){
$this->add_control(
'media_category',
[
'label' => esc_html__( 'Categories', 'plugin-name'),
'type' => \Elementor\Controls_Manager::SELECT2,
'multiple' => true,
'options' => $cd_arr,
]
);
}
$this->end_controls_section();
}
I had a class Site link to my class Annonce.
I deleted everything about "Site", did the migration with doctrine.
Then this error appeared.
So i did a find in all the file to see if "site" it's somewhere and delete everywhere.
And i still have this error.
I don't know what to do ?
Can have a cache problem ?
This is the stack Trace
ReflectionException:
Property App\Entity\Annonce::$site does not exist
at vendor/doctrine/persistence/lib/Doctrine/Common/Persistence/Mapping/RuntimeReflectionService.php:63
at ReflectionProperty->__construct('App\\Entity\\Annonce', 'site')
(vendor/doctrine/persistence/lib/Doctrine/Common/Persistence/Mapping/RuntimeReflectionService.php:63)
at Doctrine\Common\Persistence\Mapping\RuntimeReflectionService->getAccessibleProperty('App\\Entity\\Annonce', 'site')
(vendor/doctrine/orm/lib/Doctrine/ORM/Mapping/ClassMetadataInfo.php:970)
at Doctrine\ORM\Mapping\ClassMetadataInfo->wakeupReflection(object(RuntimeReflectionService))
(vendor/doctrine/orm/lib/Doctrine/ORM/Mapping/ClassMetadataFactory.php:750)
at Doctrine\ORM\Mapping\ClassMetadataFactory->wakeupReflection(object(ClassMetadata), object(RuntimeReflectionService))
(vendor/doctrine/persistence/lib/Doctrine/Common/Persistence/Mapping/AbstractClassMetadataFactory.php:181)
at Doctrine\Common\Persistence\Mapping\AbstractClassMetadataFactory->getMetadataFor('App\\Entity\\Annonce')
(vendor/doctrine/orm/lib/Doctrine/ORM/EntityManager.php:283)
at Doctrine\ORM\EntityManager->getClassMetadata('App\\Entity\\Annonce')
(vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php:2692)
at Doctrine\ORM\UnitOfWork->createEntity('App\\Entity\\Categorie', array('id' => 1, 'nom' => 'Bar Tabac PMU'), array('deferEagerLoad' => true, 'fetchAlias' => 'e'))
(vendor/doctrine/orm/lib/Doctrine/ORM/Internal/Hydration/ObjectHydrator.php:271)
at Doctrine\ORM\Internal\Hydration\ObjectHydrator->getEntity(array('id' => 1, 'nom' => 'Bar Tabac PMU'), 'e')
(vendor/doctrine/orm/lib/Doctrine/ORM/Internal/Hydration/ObjectHydrator.php:492)
at Doctrine\ORM\Internal\Hydration\ObjectHydrator->hydrateRowData(array('id_0' => '1', 'nom_1' => 'Bar Tabac PMU'), array())
(vendor/doctrine/orm/lib/Doctrine/ORM/Internal/Hydration/ObjectHydrator.php:162)
at Doctrine\ORM\Internal\Hydration\ObjectHydrator->hydrateAllData()
(vendor/doctrine/orm/lib/Doctrine/ORM/Internal/Hydration/AbstractHydrator.php:152)
at Doctrine\ORM\Internal\Hydration\AbstractHydrator->hydrateAll(object(PDOStatement), object(ResultSetMapping), array())
(vendor/doctrine/orm/lib/Doctrine/ORM/AbstractQuery.php:976)
at Doctrine\ORM\AbstractQuery->executeIgnoreQueryCache(null, null)
(vendor/doctrine/orm/lib/Doctrine/ORM/AbstractQuery.php:922)
at Doctrine\ORM\AbstractQuery->execute()
(vendor/symfony/doctrine-bridge/Form/ChoiceList/ORMQueryBuilderLoader.php:50)
at Symfony\Bridge\Doctrine\Form\ChoiceList\ORMQueryBuilderLoader->getEntities()
(vendor/symfony/doctrine-bridge/Form/ChoiceList/DoctrineChoiceLoader.php:85)
at Symfony\Bridge\Doctrine\Form\ChoiceList\DoctrineChoiceLoader->loadChoiceList(array(object(IdReader), 'getIdValue'))
(vendor/symfony/form/ChoiceList/LazyChoiceList.php:62)
at Symfony\Component\Form\ChoiceList\LazyChoiceList->getChoices()
(vendor/symfony/form/ChoiceList/Factory/DefaultChoiceListFactory.php:53)
at Symfony\Component\Form\ChoiceList\Factory\DefaultChoiceListFactory->createView(object(LazyChoiceList), array(), array('Symfony\\Bridge\\Doctrine\\Form\\Type\\DoctrineType', 'createChoiceLabel'), array('Symfony\\Bridge\\Doctrine\\Form\\Type\\DoctrineType', 'createChoiceName'), null, null)
(vendor/symfony/form/ChoiceList/Factory/PropertyAccessDecorator.php:193)
at Symfony\Component\Form\ChoiceList\Factory\PropertyAccessDecorator->createView(object(LazyChoiceList), array(), array('Symfony\\Bridge\\Doctrine\\Form\\Type\\DoctrineType', 'createChoiceLabel'), array('Symfony\\Bridge\\Doctrine\\Form\\Type\\DoctrineType', 'createChoiceName'), null, null)
(vendor/symfony/form/ChoiceList/Factory/CachingFactoryDecorator.php:127)
at Symfony\Component\Form\ChoiceList\Factory\CachingFactoryDecorator->createView(object(LazyChoiceList), array(), array('Symfony\\Bridge\\Doctrine\\Form\\Type\\DoctrineType', 'createChoiceLabel'), array('Symfony\\Bridge\\Doctrine\\Form\\Type\\DoctrineType', 'createChoiceName'), null, null)
(vendor/symfony/form/Extension/Core/Type/ChoiceType.php:410)
at Symfony\Component\Form\Extension\Core\Type\ChoiceType->createChoiceListView(object(LazyChoiceList), array('block_name' => null, 'disabled' => false, 'label_format' => null, 'label_translation_parameters' => array(), 'attr_translation_parameters' => array(), 'translation_domain' => null, 'auto_initialize' => true, 'trim' => false, 'property_path' => null, 'mapped' => true, 'by_reference' => true, 'inherit_data' => false, 'method' => 'POST', 'action' => '', 'post_max_size_message' => 'The uploaded file was too large. Please try to upload a smaller file.', 'allow_file_upload' => false, 'help_translation_parameters' => array(), 'error_mapping' => array(), 'invalid_message' => 'This value is not valid.', 'invalid_message_parameters' => array(), 'allow_extra_fields' => false, 'extra_fields_message' => 'This form should not contain extra fields.', 'csrf_protection' => true, 'csrf_field_name' => '_token', 'csrf_message' => 'The CSRF token is invalid. Please try to resubmit the form.', 'csrf_token_manager' => object(CsrfTokenManager), 'csrf_token_id' => null, 'multiple' => false, 'expanded' => false, 'block_prefix' => null, 'label' => 'Catégorie de commerce *', 'row_attr' => array(), 'attr' => array(), 'data_class' => null, 'empty_data' => '', 'required' => true, 'error_bubbling' => false, 'label_attr' => array(), 'compound' => false, 'upload_max_size_message' => object(Closure), 'help' => null, 'help_attr' => array(), 'help_html' => false, 'validation_groups' => null, 'constraints' => array(), 'choices' => null, 'query_builder' => null, 'class' => 'App\\Entity\\Categorie', 'em' => object(EntityManager), 'id_reader' => object(IdReader), 'choice_loader' => object(DoctrineChoiceLoader), 'choice_label' => array('Symfony\\Bridge\\Doctrine\\Form\\Type\\DoctrineType', 'createChoiceLabel'), 'choice_name' => array('Symfony\\Bridge\\Doctrine\\Form\\Type\\DoctrineType', 'createChoiceName'), 'choice_value' => array(object(IdReader), 'getIdValue'), 'choice_attr' => null, 'preferred_choices' => array(), 'group_by' => null, 'placeholder' => 'Choisir une catégorie', 'choice_translation_domain' => false))
(vendor/symfony/form/Extension/Core/Type/ChoiceType.php:185)
at Symfony\Component\Form\Extension\Core\Type\ChoiceType->buildView(object(FormView), object(Form), array('block_name' => null, 'disabled' => false, 'label_format' => null, 'label_translation_parameters' => array(), 'attr_translation_parameters' => array(), 'translation_domain' => null, 'auto_initialize' => true, 'trim' => false, 'property_path' => null, 'mapped' => true, 'by_reference' => true, 'inherit_data' => false, 'method' => 'POST', 'action' => '', 'post_max_size_message' => 'The uploaded file was too large. Please try to upload a smaller file.', 'allow_file_upload' => false, 'help_translation_parameters' => array(), 'error_mapping' => array(), 'invalid_message' => 'This value is not valid.', 'invalid_message_parameters' => array(), 'allow_extra_fields' => false, 'extra_fields_message' => 'This form should not contain extra fields.', 'csrf_protection' => true, 'csrf_field_name' => '_token', 'csrf_message' => 'The CSRF token is invalid. Please try to resubmit the form.', 'csrf_token_manager' => object(CsrfTokenManager), 'csrf_token_id' => null, 'multiple' => false, 'expanded' => false, 'block_prefix' => null, 'label' => 'Catégorie de commerce *', 'row_attr' => array(), 'attr' => array(), 'data_class' => null, 'empty_data' => '', 'required' => true, 'error_bubbling' => false, 'label_attr' => array(), 'compound' => false, 'upload_max_size_message' => object(Closure), 'help' => null, 'help_attr' => array(), 'help_html' => false, 'validation_groups' => null, 'constraints' => array(), 'choices' => null, 'query_builder' => null, 'class' => 'App\\Entity\\Categorie', 'em' => object(EntityManager), 'id_reader' => object(IdReader), 'choice_loader' => object(DoctrineChoiceLoader), 'choice_label' => array('Symfony\\Bridge\\Doctrine\\Form\\Type\\DoctrineType', 'createChoiceLabel'), 'choice_name' => array('Symfony\\Bridge\\Doctrine\\Form\\Type\\DoctrineType', 'createChoiceName'), 'choice_value' => array(object(IdReader), 'getIdValue'), 'choice_attr' => null, 'preferred_choices' => array(), 'group_by' => null, 'placeholder' => 'Choisir une catégorie', 'choice_translation_domain' => false))
(vendor/symfony/form/ResolvedFormType.php:148)
at Symfony\Component\Form\ResolvedFormType->buildView(object(FormView), object(Form), array('block_name' => null, 'disabled' => false, 'label_format' => null, 'label_translation_parameters' => array(), 'attr_translation_parameters' => array(), 'translation_domain' => null, 'auto_initialize' => true, 'trim' => false, 'property_path' => null, 'mapped' => true, 'by_reference' => true, 'inherit_data' => false, 'method' => 'POST', 'action' => '', 'post_max_size_message' => 'The uploaded file was too large. Please try to upload a smaller file.', 'allow_file_upload' => false, 'help_translation_parameters' => array(), 'error_mapping' => array(), 'invalid_message' => 'This value is not valid.', 'invalid_message_parameters' => array(), 'allow_extra_fields' => false, 'extra_fields_message' => 'This form should not contain extra fields.', 'csrf_protection' => true, 'csrf_field_name' => '_token', 'csrf_message' => 'The CSRF token is invalid. Please try to resubmit the form.', 'csrf_token_manager' => object(CsrfTokenManager), 'csrf_token_id' => null, 'multiple' => false, 'expanded' => false, 'block_prefix' => null, 'label' => 'Catégorie de commerce *', 'row_attr' => array(), 'attr' => array(), 'data_class' => null, 'empty_data' => '', 'required' => true, 'error_bubbling' => false, 'label_attr' => array(), 'compound' => false, 'upload_max_size_message' => object(Closure), 'help' => null, 'help_attr' => array(), 'help_html' => false, 'validation_groups' => null, 'constraints' => array(), 'choices' => null, 'query_builder' => null, 'class' => 'App\\Entity\\Categorie', 'em' => object(EntityManager), 'id_reader' => object(IdReader), 'choice_loader' => object(DoctrineChoiceLoader), 'choice_label' => array('Symfony\\Bridge\\Doctrine\\Form\\Type\\DoctrineType', 'createChoiceLabel'), 'choice_name' => array('Symfony\\Bridge\\Doctrine\\Form\\Type\\DoctrineType', 'createChoiceName'), 'choice_value' => array(object(IdReader), 'getIdValue'), 'choice_attr' => null, 'preferred_choices' => array(), 'group_by' => null, 'placeholder' => 'Choisir une catégorie', 'choice_translation_domain' => false))
(vendor/symfony/form/ResolvedFormType.php:145)
at Symfony\Component\Form\ResolvedFormType->buildView(object(FormView), object(Form), array('block_name' => null, 'disabled' => false, 'label_format' => null, 'label_translation_parameters' => array(), 'attr_translation_parameters' => array(), 'translation_domain' => null, 'auto_initialize' => true, 'trim' => false, 'property_path' => null, 'mapped' => true, 'by_reference' => true, 'inherit_data' => false, 'method' => 'POST', 'action' => '', 'post_max_size_message' => 'The uploaded file was too large. Please try to upload a smaller file.', 'allow_file_upload' => false, 'help_translation_parameters' => array(), 'error_mapping' => array(), 'invalid_message' => 'This value is not valid.', 'invalid_message_parameters' => array(), 'allow_extra_fields' => false, 'extra_fields_message' => 'This form should not contain extra fields.', 'csrf_protection' => true, 'csrf_field_name' => '_token', 'csrf_message' => 'The CSRF token is invalid. Please try to resubmit the form.', 'csrf_token_manager' => object(CsrfTokenManager), 'csrf_token_id' => null, 'multiple' => false, 'expanded' => false, 'block_prefix' => null, 'label' => 'Catégorie de commerce *', 'row_attr' => array(), 'attr' => array(), 'data_class' => null, 'empty_data' => '', 'required' => true, 'error_bubbling' => false, 'label_attr' => array(), 'compound' => false, 'upload_max_size_message' => object(Closure), 'help' => null, 'help_attr' => array(), 'help_html' => false, 'validation_groups' => null, 'constraints' => array(), 'choices' => null, 'query_builder' => null, 'class' => 'App\\Entity\\Categorie', 'em' => object(EntityManager), 'id_reader' => object(IdReader), 'choice_loader' => object(DoctrineChoiceLoader), 'choice_label' => array('Symfony\\Bridge\\Doctrine\\Form\\Type\\DoctrineType', 'createChoiceLabel'), 'choice_name' => array('Symfony\\Bridge\\Doctrine\\Form\\Type\\DoctrineType', 'createChoiceName'), 'choice_value' => array(object(IdReader), 'getIdValue'), 'choice_attr' => null, 'preferred_choices' => array(), 'group_by' => null, 'placeholder' => 'Choisir une catégorie', 'choice_translation_domain' => false))
(vendor/symfony/form/Form.php:1030)
at Symfony\Component\Form\Form->createView(object(FormView))
(vendor/symfony/form/Form.php:1033)
at Symfony\Component\Form\Form->createView()
(src/Controller/AnnonceController.php:60)
at App\Controller\AnnonceController->newFree(object(Request))
(vendor/symfony/http-kernel/HttpKernel.php:151)
at Symfony\Component\HttpKernel\HttpKernel->handleRaw(object(Request), 1)
(vendor/symfony/http-kernel/HttpKernel.php:68)
at Symfony\Component\HttpKernel\HttpKernel->handle(object(Request), 1, true)
(vendor/symfony/http-kernel/Kernel.php:198)
at Symfony\Component\HttpKernel\Kernel->handle(object(Request))
(public/index.php:22)
Thanks
It is definitely a cache issue. Try the following in order and see if it helps:
bin/console cache:clear
rm the var/cache folder
manually clear any additional PHP cache you might have enabled
I also has this same issue. I can confirm that it is an issue with the DB cache.
Before running migrations you should run this commands:
bin/console doctrine:cache:clear-query --flush
bin/console doctrine:cache:clear-result --flush
bin/console doctrine:cache:clear-metadata --flush
If you are using redis to store the DB cache you might also need to flush that before executing migrations. You can do this from the redis-cli by typing FLUSHALL
I want to retrieve the value of select input and make the test on this value. If this value corresponds to what I am looking for in my case "freelance" I display a block if no I display another
this is my code:
$emr = $this->modelManager->getEntityManager('PivotalBOBundle:Role');
$queryr = $kernel->getContainer()->get('doctrine')->getRepository('PivotalBOBundle:Role')->findAll();
$choises=array();
foreach ($queryr as $res){
$choises[$res->getRole()]=$res->getRole();
}
->tab('Spécifique')
->with('Type d\'utilisateur', array('class' => 'col-md-12'))
->add('type', 'choice', array('label' => 'Type',
'choices' => $choises))
->end()
->with('Freelancer', array('class' => 'col-md-12 Freelancer'))
->add('type', 'checkbox', array('required' => false, 'label' => 'Freelancer'))
->add('categories', 'sonata_type_model', array(
"multiple" => true,
'label' => 'Catégorie',
'required' => true,
'query' => $query))
->add('outilsEtTechnologie', 'sonata_type_model', array(
'required' => false,
"multiple" => true,
'label' => 'Outils et Technologie'))
->add('niveauCompetences', 'sonata_type_collection', array(
'required' => false,
'label' => 'Niveau des compétences',
'by_reference' => true), array(
'edit' => 'inline',
'inline' => 'table'
))
->add('niveauLangue', 'sonata_type_collection', array(
'required' => false,
'label' => 'Niveau des langues',
'by_reference' => false), array(
'edit' => 'inline',
'inline' => 'table'
))
->add('formation', 'sonata_type_collection', array(
'required' => false,
'label' => 'Formations',
'by_reference' => false), array(
'edit' => 'inline',
'inline' => 'table'
))
->add('mobilite', 'entity', array(
'class' => 'Pivotal\BOBundle\Entity\Mobilite',
'required' => true,
'label' => 'Mobilite'
))
->add('frequence', 'entity', array(
'class' => 'Pivotal\BOBundle\Entity\Frequence',
'required' => true,
'label' => 'Frequence'
))
->add('niveauExperience', 'entity', array(
'class' => 'Pivotal\BOBundle\Entity\NiveauExperience',
'required' => true,
'label' => 'Niveau Experience'))
->add('missionetranger', 'entity', array(
'class' => 'Pivotal\BOBundle\Entity\Missionetranger',
'required' => true,
'label' => 'Mission Etranger'))
->add('note', null, array('label' => 'Note'))
->end()
->with('Jobowner ', array('class' => 'col-md-12 Jobowner'))
->add('type', 'checkbox', array('required' => false, 'label' => 'Jobowner'))
->add('societe', 'entity', array(
'class' => 'Pivotal\BOBundle\Entity\Societe',
'property' => 'nom',
'label' => 'Societe',
'required' => false,
))
->end();
this is an image of my render the case 1
and this is my objectif :
the freelancer option
the jobowner option
You can access the current subject with $this->getSubject().
So you could modify your Form by using:
$subject = $this->getSubject();
// with "Type d'utilisateur"
if ($subject && $subject->getType() === 'freelance') {
// with "Freelancer"
}
// other tabs/form-fields
This only works if you gonna re-open your form view. If you need to instantly modify your form if you select "freelance" without reloading the page, you need to write it in Javascript. Depending on your needs, this varies a lot (e.g. hiding the tab/with-section and show it via Javascript).
to do this we should use a java script code like this :
->tab('Spécifique')
->with('Type d\'utilisateur', array('class' => 'col-md-12'))
->add('type', 'choice', array('label' => 'Type',
'choices' => $choises))
->end()
->with('Freelancer', array('class' => 'col-md-12 userFreelancer'))
->add('categories', 'sonata_type_model', array(
"multiple" => true,
'label' => 'Catégorie',
'required' => true,
'query' => $query))
->add('outilsEtTechnologie', 'sonata_type_model', array(
'required' => false,
"multiple" => true,
'label' => 'Outils et Technologie'))
->add('niveauCompetences', 'sonata_type_collection', array(
'required' => false,
'label' => 'Niveau des compétences',
'by_reference' => true), array(
'edit' => 'inline',
'inline' => 'table'
))
->add('niveauLangue', 'sonata_type_collection', array(
'required' => false,
'label' => 'Niveau des langues',
'by_reference' => false), array(
'edit' => 'inline',
'inline' => 'table'
))
->add('formation', 'sonata_type_collection', array(
'required' => false,
'label' => 'Formations',
'by_reference' => false), array(
'edit' => 'inline',
'inline' => 'table'
))
->add('mobilite', 'entity', array(
'class' => 'Pivotal\BOBundle\Entity\Mobilite',
'required' => true,
'label' => 'Mobilite'
))
->add('frequence', 'entity', array(
'class' => 'Pivotal\BOBundle\Entity\Frequence',
'required' => true,
'label' => 'Frequence'
))
->add('niveauExperience', 'entity', array(
'class' => 'Pivotal\BOBundle\Entity\NiveauExperience',
'required' => true,
'label' => 'Niveau Experience'))
->add('missionetranger', 'entity', array(
'class' => 'Pivotal\BOBundle\Entity\Missionetranger',
'required' => true,
'label' => 'Mission Etranger'))
->add('note', null, array('label' => 'Note'))
->end();
$formMapper->with('Societe', array('class' => 'col-md-12 Societe'))
->add('societe', 'entity', array(
'class' => 'Pivotal\BOBundle\Entity\Societe',
'property' => 'nom',
'label' => 'Societe',
'required' => false,
))
->end()
->end();
and we should overide the template
like that :
public function getTemplate($name)
{
if ($name == "edit") {
return 'admin/edit.html.twig';
}
return parent::getTemplate($name);
}
and add a javascript code in edit.html.twig :
<script type="text/javascript">
$(document).ready(function () {
var type = $("#{{ admin.uniqId }}_type");
var freelancer = $(".userFreelancer");
var jobowner = $(".userJobowner");
var redacteur = $(".userRedacteur");
var Supperjobowner = $(".userSupperjobowner");
var Nactif = $(".userNactif");
var Societe = $(".Societe");
hideUserBlock()
type.change(function () {
displayUserBlock();
}); // Bind the function to displayBlock
type.change(); // Manual trigger to display block.
function displayUserBlock() {
hideUserBlock();
var type =$("#{{ admin.uniqId }}_type").val()
switch(type) {
case 'Freelancer':
freelancer.css("display", "block");
freelancer.trigger("create");
break;
case 'Jobowner':
Societe.css("display", "block");
Societe.trigger("create");
break;
case 'Super Jobowner':
Societe.css("display", "block");
Societe.trigger("create");
break;
case 'Jobowner non actif':
Societe.css("display", "block");
Societe.trigger("create");
break;
case 'Rédacteur':
Societe.css("display", "block");
Societe.trigger("create");
break;
}
}
function hideUserBlock() {
freelancer.css("display", "none");
freelancer.trigger("create");
Societe.css("display", "none");
Societe.trigger("create");
}
});
</script>
I am trying to create my first drupal module that creates a simple node type wcNames. This node type has two fields, firstname & lastname.
I could get my module create the DB table for node type and add it in node_type table, but when I open node/add/wc-name fields firstname and lastname don't appear.
Below is my code. Please suggest.
<?php
// wc_name.install file
/*
* hook_schema Implementation: Creates the DB and it's fields
*/
function wc_name_schema(){
$schema['wc_name_names'] = array(
'fields' => array(
'nmid' => array('type' => 'serial', 'unsigned' => TRUE, 'not null' => TRUE),
'firstname' => array('type' => 'varchar', 'length' => 50, 'not null' => FALSE),
'lastname' => array('type' => 'varchar', 'length' => 50, 'not null' => FALSE),
),
'primary key' => array('nmid'),
'unique keys' => array(),
'indexes' => array(
'nmid' => array('nmid'),
),
);
return $schema;
}
//function wc_name_install(){ } // Use this function to set variables for the module.
function wc_name_uninstall(){
//unset variables if any set
// Delete all wc_name nodes
db_delete('node')->condition('type', 'wc_name')->execute();
// Delete all wc_name tables
db_drop_table('wc_name_names');
}
?>
<?php
// wc_name.module
/*
* Implementation of _node_info(): define node
*/
function wc_name_node_info(){
return array(
'wc_name' => array(
'name' => t('wcName'),
'base' => 'wc_name',
'base' => 'page',
'description' => t("A smaple module to save names."),
'has_title' => TRUE,
'title_label' => t('Title'),
'has_body' => FALSE,
)
);
}
function wc_name_menu() {
$items = array();
$items['wc_name'] = array(
'title' => 'wcNames',
'page callback' => 'wc_name',
// 'access arguments' => array('access wcNames'),
);
}
?>
<?php
// wc_name_form.inc
function wc_name_form($node, &$form_state){
// Add fields
$form['first_name'] = array(
'#type' => 'textfield',
'#title' => t('First name'),
'#required' => FALSE,
'#default_value' => $node->wc_name['firstname'],
);
$form['last_name'] = array(
'#type' => 'textfield',
'#title' => t('Last name'),
'#required' => FALSE,
'#default_value' => $node->wc_name['lastname'],
);
return $form;
}
?>
What am I missing?
Try this:
function createTextFieldByName($type, $fieldname, $fieldlabel)
{
$field = field_info_field($fieldname);
if (empty($field)) {
$field = array(
'field_name' => $fieldname,
'type' => 'text',
'entity_types' => array('node'),
'translatable' => TRUE,
);
$field = field_create_field($field);
}
$instance = field_info_instance('node', $fieldname, $type);
if (empty($instance)) {
$instance = array(
'field_name' => $fieldname,
'entity_type' => 'node',
'bundle' => $type,
'label' => $fieldlabel,
);
$instance = field_create_instance($instance);
return $instance;
}
return null;
}
$type =
array(
'type' => 'MY_TYPE',
'name' => t('NAME'),
'base' => 'node_content',
'description' => t("DESCRIPTION"),
'custom' => 1,
'modified' => 1,
'locked' => 0,
);
$type = node_type_set_defaults($type);
node_type_save($type);
createTextFieldByName($type,'firstname','First name');
createTextFieldByName($type,'lastname','Last name');