I'm making a Flutter application.
But, I cannot delete a field in the Firestore document.
In another language I know to use FieldValue.delete() to delete a file in Firestorm document.
In Dart, How do I delete?
Update Oct,2018: This is Now Possible:
In order to delete a particular field from a Cloud Firestore document - make sure you are using Plugin version 0.8.0 or Above. Now a E.g If you have a document having a field 'Desc' with contain some Text. In Order to Delete it.
Firestore.instance.collection('path').document('name').update({'Desc': FieldValue.delete()}).whenComplete((){
print('Field Deleted');
});
This will Delete 'Desc' Field from the Document 'name'
I think this is currently impossible in standard, non hacky way.
There is an open issue https://github.com/flutter/flutter/issues/13905 in Flutter which have to be resolved first.
Get the DocumentReference, invoke update with a map which has a key you want to delete and value FieldValue.delete().
var collection = FirebaseFirestore.instance.collection('collection');
collection
.doc('document_id')
.update({
'field_to_delete': FieldValue.delete(),
}
);
Here is an official solution for deleting Field Path when programming in Flutter/Dart
Here is how a sample Firestore collection looks like:
{
'path': {
'name': {
'title': 'my title'
'description': 'my description',
}
}
}
In order to delete description field, you can do this:
Firestore.instance.collection('path').document('name').set(
{'description': FieldValue.delete()},
SetOptions(
merge: true,
),
)
And the collection will look like this:
{
'path': {
'name': {
'title': 'my title'
}
}
}
if you have nested fields then use the '.' (Dot) notation to specify the field.
E.g if your data is nested Map then this is handy.
Firestore.instance.collection('path').document('name').update({'address.town': FieldValue.delete()}).whenComplete((){
print('Field Deleted');
});
Related
This is follow up question on Querying wordpress with meta queries from Gatsby
After a bit debugging I've gathered, and please correct me if I'm wrong, that Gatsby on build downloads the entire data structure and caches it. So all the GraphQL queries are performed against the cache. This makes all adjustments I try to make to wordpress (for example https://www.wpgraphql.com/2020/04/14/query-posts-based-on-advanced-custom-field-values-by-registering-a-custom-where-argument/) useless. I'm confined to using the filter argument for my GraphQL queries in Gatsby.
Consider the following query:
query Test {
allWpPage(filter: {pagesGeo: {}}) {
edges {
node {
pagesGeo {
genericPage {
... on WpPage {
id
}
}
hreflangValue
}
}
}
}
}
In this case I want to filter on genericPage, but it's not in the list of available filters in the GraphiQL query tester.
In Wordpress the custom field generic_page is defined with the help of advanced custom fields and it's of the field type 'Post Object'. As you can see I'm able to query the field just fine, and it would be easy for me to create a meta query in Wordpress to filter on the field. It would looks something like:
$query_args['meta_query'] = [
"relation" => "OR",
[
'key' => 'generic_page',
'value' => $postObjectId,
'compare' => '='
],
[
'key' => 'generic_page',
'value' => $postObjectId2,
'compare' => '='
],
];
Is there a way to make it possible for me to filter on genericPage in Gatsby?
If not, are there any alternative solutions for me to extract the data I need?
After a bit debugging I've gathered, and please correct me if I'm
wrong, that Gatsby on build downloads the entire data structure and
caches it. So all the GraphQL queries are performed against the cache.
Yes, that's correct.
And I don't know much about Gatsby or GraphQL coding, but this is what I did and it worked quite well for me:
I created a Gatsby site using the "WordPress blog" starter: https://github.com/gatsbyjs/gatsby-starter-wordpress-blog
Then on my WordPress site, I installed these plugins: ACF (free version, v5.10.2), WP Gatsby v1.1.3, WP GraphQL v1.6.4, "WPGraphQL for Advanced Custom Fields" v0.5.3, and (just for testing) WPGraphQL Meta Query v0.1.0
Then I created an ACF field group named "Pages GEO":
In the GraphQL meta box, I set "Show in GraphQL" to "Yes" and then I set "GraphQL Field Name" to pagesGeo (which is what you used, right?)
Then I created a Post Object field named "Generic Page" (generic_page) and then I set "Show in GraphQL" to "Yes"
And then of course, I created/edited some Pages (i.e. post type page) and then selected a "Generic Page" for those Pages.
So regarding this:
In this case I want to filter on genericPage, but it's not in the list
of available filters in the GraphiQL query tester.
I was not able to add filter.pagesGeo.genericPage, but I managed to filter by that field (i.e. filter.genericPage) by doing this:
I registered my very own custom WP GraphQL field named genericPage
In my theme functions.php, I added:
The code is based on the one here.
add_action( 'graphql_register_types', function() {
register_graphql_field( 'Page', 'genericPage', [
'type' => 'Integer',
'description' => 'generic_page meta value',
'resolve' => function( \WPGraphQL\Model\Post $post ) {
return (int) get_post_meta( $post->databaseId, 'generic_page', true );
}
] );
});
Then in GraphiQL IDE (at http://localhost:8000/__graphql), I ran this query:
So in the above query, I filtered the posts by the generic_page meta, where I queried for posts where the meta value (i.e. the post ID, or databaseId in GraphQL) is 2 or 82.
And I used the in comparator, but you could just use any other comparator that suits your query. See https://www.gatsbyjs.com/docs/query-filters/#supported-comparators
And the result was:
So as you could see, for each node/post (in data.allWpPage.nodes), the root genericPage matches the pagesGeo.genericPage.databaseId.
Additional Notes
I used the gatsby develop command which (if I was not mistaken) rebuilds the cache, but if necessary, the cache can be cleared manually — see https://www.gatsbyjs.com/docs/build-caching/#clearing-cache.
I have a collection 'posts' which has documents as uid of the particular users, under each document I have array 'posts' which contains a string 'likes' and a map 'post' which again contain a string 'userpost'.
I need to show data of the 'userpost' as a list in my homepage. Can someone suggest me a query for this.
I tried this:
return Firestore.instance.collection('posts').where('posts', arrayContains: 'post').snapshot();
And in my home page under listview.builder I'm retrieving data like this:-
Text( snapshot.data.documents[i].data['userpost'], )
But after running It isn't showing anything in the homepage and an execption is thrown: A build function returned null.
Firestore QuerySnapshot which you have to loop over to get the actual document map
Should try this
snapshot.data.documents.map((DocumentSnapshot doc){
if(doc.data.hasdata)){
return Text( doc.data.data['userpost'], );
}
return Text('Loading');
}).toList()
I want to perform bulk update of users with a Approved users, the table
field_user_status_value
-----------------------
entity_type, entity_id, field_user_status_value
The entity_id is the user id which does not exist in the table, below is the custom module I wrote to update the table:
function bulkapprove_action_info() {
return array(
'bulkapprove_action_callback_name' => array(
'type' => 'user', // Can be file, term, user, etc.
'label' => t('Approve User'),
'configurable' => FALSE, // Doesn't need config form
'behavior' => array('view_property'), // Uses view access rights ,
'pass rows' => TRUE,
'triggers' => array('any'), // Works always
),
);
}
function bulkapprove_action_callback_name($entity, $context)
{
db_update('field_data_field_user_status')->fields(array('field_user_status_value' => 'Approved'))->condition('entity_id', $context->entity_id)->execute();
}
But it is not inserting the values in this table
In Drupal you do not want to update the database fields directly unless you created the table. Drupal's internal APIs provide a collection of tools to ensure you update the values correctly and that all supporting modules get notified of changes as needed through the hook system.
In this case the callback gets the actual entity to run your action against (in this case the user object). You want to take action on that entity and then save the entity.
function bulkapprove_action_callback_name($entity, $context)
{
$entity->status = 1;
entity_save('user', $entity);
}
currently I'm using sonata admin bundle to export a "order" data, how can I export the data with manytomany relationship? I saw a post, it helps a bit, but I still not sure how to get the data in many to many relationship.
Here is my code:
public function getExportFields() {
return [
$this->getTranslator()->trans('Order Number') => 'id',
$this->getTranslator()->trans('First Name') => 'customer.First_name',
$this->getTranslator()->trans('Last Name') => 'customer.Last_name',
...]
Here is fine, but when I try to get 'OrderToProduct' or 'product.name' it failed or only output empty string. I spent to much time on this already, hope someone can give a clue. Thank you.
Well, you can't use product.name, because product is a collection.
Export action iterates through objects and get properties with path defined and tries to stringify them.
What you need to do is a workaround - define some method in Order class - i.e. getProductsAsString(), make it return string you need, and then add
$this->getTranslator()->trans('Products') => 'productsAsString'
But still - it will put whole string in single cell of xls, csv, xml you are trying to export.
Goal: Is to save information of a node which gets updated. We need to gather the node id of the node which is updated and also the user names of people who have bookmarked it.
Implementation:
I have managed to get the both this detail using flags and rules module. I made a custom module which implemented the hook to get this info.
I am getting stuck here:
Now I need to save the user name and the node id. I am still deciding if I want to use fields or the db layer.
One username can have multiple node id saved.
Now the problem is I don't know for sure how many nodes will be enough. It depends on the user. It can be 5 can be 500 or even 5000 node ids that might need to be saved for one user.
So how do I make provision for this ?
So I am stuck with the logic. How should I use the db layer or the fields in custom content type to save this ? and how should I do it ?
Please advice. I am using d7.
custom module code
/*
* Implementation of the hook_rules_action_info()
*
*/
function customvishal_rules_action_info()
{
$actions = array(
'customvishal_action_userdetail' => array(
'label' =>t('Custom function to send notifications'),
'group'=>t('Cusotm Code for sending notifications'),
'parameter'=> array(
'account'=> array(
'type'=>'user',
'label'=>t('Going to get user list'),
),
// for the node
'productdetail'=> array(
'type'=>'node',
'label'=>t('Passding the node data'),
),
)
),
);
return $actions;
}
/*
* The action function for the rules exampled hello world
*
*/
function customvishal_action_userdetail($account,$productdetail)
{
drupal_set_message(t('This user #username! has flagged it',
array('#username' => $account->mail)));
drupal_set_message(t('This node #nid has got updated',
array('#nid' => $productdetail->nid)));
// The above takes care of the node and the user information later I will put
// it in a db or something like that.
// end of the function customvishal_action_userdetail
}
It really seems like you should be using hook_node_update() and hook_node_insert() to for access to nodes that have just been added or updated.
If you wanted access to the node data just before it was saved, then hook_node_presave() would be the one to use.
I don't think you need presave though because you mentioned you needed the node ID, and presave does not have that for new nodes yet.
Here's a way to process new and updated nodes. The first 2 functions just hook into the right place and route the node to the 3rd function.
<?php
// hook into node inserts
function customvishal_node_insert($node) {
if ($node->type == 'mynodetype') {
customvishal_handle_data($node);
}
}
// hook into node updates
function customvishal_node_update($node) {
if ($node->type == 'mynodetype') {
customvishal_handle_data($node);
}
}
// custom handler for the nodes
function customvishal_handle_data($node) {
// load a user object of the node's author
$author = user_load($node->uid);
// now do what we need to do with $node and $user data
}
Remember you need to clear the Drupal cache for new hooks in your module to work in D7.