How to add user-fields programmatically in Drupal 7 - drupal

I'm trying to create a .install for a module that I'm migrating from Drupal 6. It requires two 'profile fields', which, in drupal 6, it checked for and created automatically.
To upgrade it to drupal 7 I'm trying to do this with fields! Easy enough right?
So far I have
if(!field_info_field('user_fullname')) {
$field = array(
'field_name' => 'user_fullname',
'type' => 'text',
'settings' => array(
'required' => TRUE,
),
);
field_create_field($field);
$instance = array(
'field_name' => 'user_fullname',
'entity_type' => 'user',
'label' => 'The user\'s full name',
'bundle' => 'additional_info',
'required' => true,
'widget' => array(
'type'=>'options_select',
)
);
field_create_instance($instance);
}
Which, sure enough, creates the field, but it's not visible in the user's profile?
Do I need something additional for that? If so, What?
Many Thanks.
SOLVED: It was due to the bundle (not entirely sure what a bundle is really), I changed the bundle to 'user' and it appeared!

bundle is pretty much the same as content type. But since in D7 users are entities too, but they are not content, using the term 'content type' didn't make sense. Reference: Barry Jaspan's DrupalCon Paris 2009 Session: Intro to the Field API for Module Developers.
Intro to the Field API for Module Developers

It was due to the bundle (not entirely sure what a bundle is really), I changed the bundle to 'user' and it appeared!

As I know in D7 the bundles are something like a model for an entity and it can have fields. The default drupal bundles are node, user and taxonomy but the new API provides the developer to create custom bundles too. Every field needs to belong to a bundle.

A bundle in Drupal is a subset of the entity. In this case the entity type is User and there is only one type of User so the bundle is User.
In Taxonomy: the Taxonomy is the Entity and the Vocabularies are the bundles.
In Nodes: Nodes are the Entity and Content Types are the bundles.
No field can be attached to an entity, properties are attached to the entity (published, sticky, etc). Fields are attached to the Bundles.
I don't have enough rep to comment, so here is my answer for those you find this via google. As I did.

Related

Implementing cmb_field_map for cmb2

I'm trying to use the cmb_field_map extension by julykaz for cmb2 in a wordpress plugin to display a google map location.
https://packagist.org/packages/julykaz/cmb_field_map
Have successfully installed it into composer and action hook for cmb2_render_pw_map is registering in the page - but no field (or map) displayed.
cmb2 is installed and working correctly. By the documentation on the page, I should just have to add the appropriate type.
I've got:
$cmb_location->add_field( array(
'name' => 'Google Map Location',
'desc' => 'Drag the marker to set the exact location',
'id' => $prefix . 'map',
'type' => 'pw_map'
) );
In the admin edit page the 'name' field is display but nothing for the googlemap (or even a field).
Not sure what I'm missing here...
Look this -> https://github.com/mustardBees/cmb_field_map Propably you've probably forgotten Google Maps API key.

Woocommerce REST API Update Product Categories

I'm using Wordpress 4.9.5 with Woocommerce 3.3.5. I am using the WooCommerce REST API PHP Client Library to update products on the website when they are changed in a separate product management system. That library is using v2 of the REST API.
Using the following code I am successfully updating the basic product data (title, description, sku, price etc) but I can't get the categories to update from Uncategorized. The categories are also not set when using similar code to create a product if it doesn't already exist on the site.
$client = new WC_API_Client( $domain, $consumerKey, $consumerSecret, $options );
$client->products->update( $id, array(
'sku' => $product->sku,
'title' => $product->title,
'type' => $product->type,
'status' => $product->status,
'regular_price' => $product->regular_price,
'description' => $product->description,
'categories' => array(
array(
'id' => 343
),
array(
'id' => 347
)
)
));
As I say, the other fields update as expected. I have confirmed that categories with IDs 343 and 347 definitely exist so I assume I must have a problem with the syntax. As the other fields update the authentication is definitely working.
I have read the official Woocommerce API documentation and based my code on this tutorial. Based on both of those, I'm not sure what I have done wrong.
Thanks for any help or advice.
I resolved this in the end. It was on oversight on my part.
The client library I was using was connecting to what the Woocommerce documentation calls the 'Legacy v2' version rather than the 'v2' version of the API. Categories, image alt tags, meta data etc are not supported in the legacy versions.
I switched from the using library to connecting directly to the 'v2' version using https://sitename/wp-json/wc/v2/endpoint and all is now well.

How can I list Content Types as checkbox in Profile types for Drupal

In drupal user registration, Users should be able to choose the types which they want to subscribe to. So, how can I list Content Types (node types) in Custom Profile Registration Type?
http://i.stack.imgur.com/hvWd2.png
You can retrieve all Content Types with node_type_get_names() function.
So, in hook_form_alter add:
$form['ct'] = array(
'#type' => 'checkboxes',
'#options' => node_type_get_names(),
'#title' => t('What standardized tests did you take?'),
);

Bundle for notifications in Symfony2

I want to include notifications, e.g. for forum threads. A user shall retrieve notifications if there are any new posts to a forum thread. Another example would be the notifications on Facebook, e.g. where you're notified of new comments on your posts or pictures.
Is there a notification bundle for Symfony2 that you can recommend for implementing such a function?
I came up with the Sonata NotificationBundle but I am unsure if this is what I really need. When I look at the usage examples it looks as if this would provide a email notification function
// retrieve the notification backend
$backend = $container->get('sonata.notification.backend');
// create and publish a message
$backend->createAndPublish('mailer', array(
'from' => array(
'email' => 'no-reply#sonata-project.org',
'name' => 'No Reply'
),
'to' => array(
'myuser#example.org' => 'My User',
'myuser1#example.org' => 'My User 1',
),
'message' => array(
'html' => '<b>hello</b>',
'text' => 'hello'
),
'subject' => 'Contact form',
));
or a logging function
$this->get('sonata.notification.backend')->createAndPublish('logger', array(
'level' => 'debug',
'message' => 'Hello world!'
));
Can you confirm/recommend the usage of that bundle? Or can you recommend any other?
Good news:
NotificationBundle from GeniusesOfSymfony
https://github.com/GeniusesOfSymfony/NotificationBundle
Proper documentation will probably be published soon.
(on-going discussion here: https://github.com/GeniusesOfSymfony/WebSocketBundle/issues/4#issuecomment-81829513)
I've already tested a similar bundle created by them (WebSocketBundle) which also provides real-time-interaction and it works really great. The documentation is also very good.
I recommend that you "star" the project on github. You can also create an issue to ask about the current status of the project.
I'm searching for the same library now, and also found https://github.com/namshi/notificator
Didn't use it yet, but it has 130 stars today, so probably will try it.
You can achieve this with sonata notification bundle.
You will need to create custom consumer class as described here
https://sonata-project.org/bundles/notification/3-x/doc/reference/usage.html#custom-consumer
You can try this Bundle: NotificationsBundle
It's a simple implementation for Pusher that provides a real time data proadcast.

Help for custom menu

Drupal 7 hook_menu() is confusing me; I have tried everything and I can't seem to get this to work.
What I need: In a custom module, I'd like to create a new menu, and add about four links to that menu. It sounds simple, but I am struggling. I've been able to create the menu itself using the $menu array in the .install file, but adding items to that menu doesn't make sense.
Code that is working:
$menu = array(
'menu_name' => 'project-menu',
'title' => $t('Project Menu'),
'description' => 'Project Menu',
);
menu_save($menu);
Code that isn't working:
$items = array();
$items['project-menu/%'] = array(
'title' => 'Test Link',
'page callback' => 'dc_project_page',
'page arguments' => array(1),
'access callback' => TRUE,
'type' => MENU_LOCAL_TASK,
);
return $items;
This is all in the dc_project.install file under the dc_project_menu() function. Hopefully I'm just doing something stupid, any and all help is extremely appreciated. Even just pointing to me to a module that does this cleanly as an example, thanks. I did look at the example project, haven't been able to get anything as far as adding links to my new menu working.
Passing to menu_save() the content of $items doesn't work because menu_save() accepts only an array containing menu_name, title, and description.
What you use in $items is an array describing the menu callbacks implemented by a module, and the definitions of the menu callbacks implemented by all the modules are not saved in "menu_custom" (the table used from menu_save()) but are cached in a Drupal cache table.
If you are trying to change the menu callbacks defined by another module, then you should implement hook_menu_alter(); otherwise, if you just want do define the menu callbacks of your module, you should implement hook_menu().
Both the hooks implementations (hook_menu() and hook_menu_alter()) must be in the module file (in your case, in dc_project.module), not in dc_project.install. Drupal doesn't load the installation file when it normally loads the enabled modules; it loads the installation file when a module is being updated (or installed), but it doesn't load it in other cases.
The code that saves the menu with menu_save() can be in the installation file, in the implementation of hook_install() or hook_update_N(). It could also be put in the implementation of hook_enable(); in that case, the code (which is executed when the module is enabled) should first verify the menu has not been already added. (hook_enable() and hook_disable() should be placed in the installation file.)

Resources