I'm using MB Relationships plugin to create a relationship between two custom post types movies and reviews
Register relationship:
function register_movies_to_reviews_relationship()
{
MB_Relationships_API::register([
'id' => 'movies_to_reviews',
'from' => 'movies',
'to' => 'reviews',
]);
}
add_action('mb_relationships_init', 'register_movies_to_reviews_relationship');
=> It's working good
Fetch the reviews data by movie_id:
$reviews = MB_Relationships_API::get_connected([
'id' => 'movies_to_reviews',
'from' => $movie_id
]);
=> It's working good
But i don't know how i can fetch the movies data by review_id.
How can i do that? Somebody can help me. Thank you!!!
Shouldnt it be just replace from/to with the according id?
$reviews = MB_Relationships_API::get_connected([
'id' => 'movies_to_reviews',
'to' => $review_id
]);
Related
I'm using current commerce 2.x.dev for online store development. It's first project with Commerce 2 for me.
When i started to work on products import, i found that Feeds module does not stable, and i decided to write custom solution for data import (Batch/Queue API data import from CSV/XML sources).
So, at this moment i cannot find any information about correct product entities creation via code. I explored Drupal Commerce documentation section: http://docs.drupalcommerce.org/v2/product/products.html but it contains only UI instructions for manual products management.
I think that short instruction for working from code with products / orders entities will be very helpful for developers, especially for developers, who starts working with commerce 2 and have some experience with 7.x commerce.
To create a product programmatically with 3 product variations, use the following code in a custom module:
use Drupal\commerce_product\Entity\ProductVariation;
use Drupal\commerce_product\Entity\Product;
use Drupal\commerce_price\Price;
function my_module_install() {
// Create variations
$variation1 = ProductVariation::create([
'type' => 'default',
'sku' => 'var1',
'price' => new Price('24.00', 'EUR'),
]);
$variation1->save();
$variation2 = ProductVariation::create([
'type' => 'default',
'sku' => 'var2',
'price' => new Price('50.00', 'EUR'),
]);
$variation2->save();
$variation3 = ProductVariation::create([
'type' => 'default',
'sku' => 'var3',
'price' => new Price('115.00', 'EUR'),
]);
$variation3->save();
// Create product using variations previously saved
$product = Product::create([
'type' => 'default',
'title' => t('Your Product Name'),
'variations' => [$variation1, $variation2, $variation3],
]);
$product->save();
}
I hope that it answers your question. Feel free for more details.
Best regards
You need to read this documentation (Creating products) and do the same way.
Edited
$variation_blue_large = \Drupal\commerce_product\Entity\ProductVariation::create([
'type' => 'my_custom_variation_type',
'sku' => '001',
'price' => new \Drupal\commerce_price\Price('10.00', 'USD'),
'attribute_color' => $blue,
'attribute_size' => $large,
])->save();
$variations = [
$variation_blue_large,
];
$product = \Drupal\commerce_product\Entity\Product::create([
'uid' => 1,
'type' => 'my_custom_product_type',
'title' => 'My Custom Product',
'stores' => [$store],
'variations' => $variations,
]);
$product->save();
**Load product with Multi-pal variation **
use Drupal\commerce_product\Entity\ProductVariation;
use Drupal\commerce_product\Entity\Product;
use Drupal\commerce_price\Price;
// Load existing variations
$result = \Drupal::entityQuery('commerce_product_variation')
->condition('type', 'variation_type')
->execute();
$entity_manager = \Drupal::entityManager();
$product_variation = $entity_manager->getStorage('commerce_product_variation')->loadMultiple($result);
//Add variation to Product
$product = Product::create([
'type' => 'hakuro_plate',
'title' => t('Your Product Name custom New testing'),
'variations' =>$product_variation,
]);
$product->save();
I need to develop one module that converts the 'Forum Topic' of the site, that is a content type, into one comment of another node of content type 'Action'. The Forum topic and the node 'action' have a taxonomy term in commun.
So I start to searching the values that I need in the DB, from table 'node', 'user'... and after I made db_insert in the table 'comment'.
function mymodule_entry_insert($entry) {
$select = db_select('node', 'n');
$select->join('users','u','u.uid = n.uid');
$select->fields('n', array('nid', 'title', 'created'));
$select->fields('u', array('name'));
$select->condition(db_or()->condition('n.type', 'forum', '='));
// Return the result in object format.
return $select->execute()->fetchAll();
$fields = array(
'nid' => $select->fields('n', array('nid')),
'uid' => $select->fields('n', array('uid')),
'subject' => $select->fields('n', array('title')),
'hostname' =>'::1',
'created' => $select->fields('n', array('created')),
'changed' => $select->fields('n', array('created')),
'status' => '1',
'language' => 'und',
'thread' => '01/',
'name' => $select->fields('u', array('name')),
'mail' => '',
'homepage' => '',);
try {
$return_value = db_insert('comment')
->fields($fields)
->execute();
}
catch (Exception $e) {
drupal_set_message(t('db_insert failed. Message = %message, query= %query',
array('%message' => $e->getMessage(), '%query' => $e->query_string)), 'error');
}
return $return_value;
}
After search during days in internet, I haven't found any solution for this that seems to be very easy. I don't have a huge experience.
The second part of the module is to convert the comments of the ex-forum, to the children comments of the new coment that has been created.
So I must to group the comments by nid ('the node to wich this comment is a reply'), and all of them except one(the father, old forum ) must to insert the cid ('primary key') of their father comment in the column 'pid' (the comment cid to wich this comment is a reply)
And the third part is to get the id of the node with same term that the forum, and change the nids of the comments affected.
I hope that I will be able to develop the second and the third part , in the same way that the first one.
Thank you :)
in my ZF2 application I want to encrypt my parameters, so that when having a link like /product/update/1 will be displayed as /product/update/cdsk45kdi340kd0wlw0 or something similar. I need to encrypt and decrypt it in controllers and views. What is the best approach to this? Thank you for any help.
The point here is that you want to avoid users to guess the url. I would generate a random token per product. Store in your database this token together with the id of the product (and all other properties).
To generate a random string as a token, you can use Zend\Math\Rand: Rand::getString(10); gives you a random string of 10 characters. When you store the products in your database, generate a random string for every product. Then, in your controller you do not get the product based on the identifier (id), but based on the token.
make route look like this
'product' => array(
'type' => 'Zend\Mvc\Router\Http\Segment',
'options' => array(
'route' => '/product/update[/:hashedid]',
'constraints' => array(
'hashedid' => '[a-zA-Z0-9-_\.]+',
),
'defaults' => array(
'controller' => 'Index',
'action' => 'index',
),
),
),
and in controller
$hashedid = $this->params()->fromRoute('hashedid', 0);
$id = $this->dehash($hashedid);
How can I retrieve all users registered in my WordPress blog having a particular meta data?
For example I have made an option to add a custom meta data for every registering users having meta key as parent_id. If I want to list all users having parent_id as 2 , then how can I do this?
Since WP v3.1 it's ridiculously easy to search for a user by his/her meta key.
Use the function
get_users($args)
(WP Documentation)
The function takes an array of parameters, in your case you need
get_users(array(
'meta_key' => 'parent_id',
'meta_value' => '42'
))
Simple way how to get one user by his metadata is:
$user = reset(
get_users(
array(
'meta_key' => $meta_key,
'meta_value' => $meta_value,
'number' => 1
)
)
);
Here is how you can get users based on a custom role and multiple metadata keys,
$available_drivers = get_users(
array(
'role' => 'driver',
'meta_query' => array(
array(
'key' => 'approved',
'value' => true,
'compare' => '=='
),
array(
'key' => 'available',
'value' => true,
'compare' => '=='
)
)
)
);
Explaining the above query, I want only those users who I assigned the role of driver, and they are approved and available. The approved and available are custom fields created using ACF as True/False fields.
If you have additional metadata to test, add another array element to the meta_query array.
Meanwhile checkout my open source at github.com/patrickingle
Here is the codex page from Wordpress detailing how to use the get_users($arg); function.
It contains examples how how to build custom functions to fetch various parts of user data. You'll have to naturally build and make some of your own changes to get it how you want.
Additionally here is a link to a function somebody built that will fetch user data based on roles within wordpress. You can configure it in many different ways with some tweeking, but this will allow you to filter your results in a more powerful manner.
1) Where is the best place to populate a new database table when a module is first installed, enabled? I need to go and get some data from an external source and want to do it transparently when the user installs/enables my custom module.
I create the schema in {mymodule}_schema(), do drupal_install_schema({tablename}); in hook_install. Then I try to populate the table in hook_enable using drupal_write_record.
I confirmed the table was created, I get no errors when hook_enable executes, but when I query the new table, I get no rows back--it's empty.
Here's one variation of the code I've tried:
/**
* Implementation of hook_schema()
*/
function ncbi_subsites_schema() {
// we know it's MYSQL, so no need to check
$schema['ncbi_subsites_sites'] = array(
'description' => 'The base table for subsites',
'fields' => array(
'site_id' => array(
'description' => 'Primary id for site',
'type' => 'serial',
'unsigned' => TRUE,
'not null' => TRUE,
), // end site_id
'title' => array(
'description' => 'The title of the subsite',
'type' => 'varchar',
'length' => 255,
'not null' => TRUE,
'default' => '',
), //end title field
'url' => array(
'description' => 'The URL of the subsite in Production',
'type' => 'varchar',
'length' => 255,
'default' => '',
), //end url field
), //end fields
'unique keys' => array(
'site_id'=> array('site_id'),
'title' => array('title'),
), //end unique keys
'primary_key' => array('site_id'),
); // end schema
return $schema;
}
Here's hook_install:
function ncbi_subsites_install() {
drupal_install_schema('ncbi_subsites');
}
Here's hook_enable:
function ncbi_subsites_enable() {
drupal_get_schema('ncbi_subsites_site');
// my helper function to get data for table (not shown)
$subsites = ncbi_subsites_get_subsites();
foreach( $subsites as $name=>$attrs ) {
$record = new stdClass();
$record->title = $name;
$record->url = $attrs['homepage'];
drupal_write_record( 'ncbi_subsites_sites', $record );
}
}
Can someone tell me what I'm missing?
If ncbi_subsites_get_subsites() is not in the .install file, you need to include whatever file its in with your module. Otherwise, it's returning nothing, in which case try dumping $subsites and exiting.
I think the answer is that drupal_write_record is not meant for install or enable hooks. I think when enabling or installing, you have to write SQL. That is the impression I am getting from reading some posts that mention that the schema is not available in these hooks.
First of all (assuming Drupal 6), drupal_write_record() cannot be called from hook_install() because Drupal would not find the database schema defined from the module, which is still going to be installed, and enabled.
Instead you need to use db_query() function. (the comments are speaking of a way to include default data by prviding it to hook_schema() serialized, but i've found no documentation on this.)
However, would you be using (the development version of) Drupal 7, you want to look at the db_insert() function instead.