Creating a Pods relationship programmatically produces error in Advanced Custom Fields - wordpress

I'm trying to create a pods relationship using the following code:
$data = array(
"pod_id" => esc_attr(strip_tags($_POST['customMetaAutorID'])),
"field_id" => 1073,
"item_id" => $post_id,
"related_item_id" => $_POST["customMetaAutorID"],
"related_pod_id" => 0,
"related_field_id" => 0,
"weight" => 0
);
$wpdb->insert("wp_podsrel", $data);
The row gets added to the table, how ever, after a few page refreshes I start getting the error:
Strict Standards: Declaration of acf_taxonomy_field_walker::start_el() should be compatible with Walker::start_el(&$output, $object, $depth = 0, $args = Array, $current_object_id = 0)
This means that all I have is the white screen of death and the only thing I can do is to restore the database.
What's the way to add a pods relationship field value and not breaking everything else?

Found out the answer myself.
Turns out each pod item has an add_to function wich adds values to related fields given the field name (much more convenient than harcoding the field ID)
The code I ended up using is this:
$postPod->add_to("field_name", $related_element_id);

Related

bbpress user-roles or capabilities per forum

I'm trying to setup a bbpress with extended user capabilities.
The problem
My goal is that users need to have different capabilities in each forum, i.e:
UserA can't access ForumW
UserA can only read topics and replies in ForumX
UserA can create topics and write replies in ForumY
UserA can moderate ForumZ
Plugins
These are the plugins I tried so far, but without success:
Ultimate Member, official 1.7 and the new 2.0 version
https://ultimatemember.com/
They claim that they're working on a groups extension for UltimateMember v2, which somehow looks promising, but as of now there's no release date and I still don't know if this extension is going to solve my problem.
itthinx Groups plugin
http://docs.itthinx.com/document/groups/
Allows me to assign multiple groups to users and forums, but there's still a catch.
First attempt
Since itthinx Groups plugin allows me to assign multiple groups to UserA, which is great, it's still not solving my issue.
So, I tried something like this:
ForumX has the following groups assigned: ForumX_readers, ForumX_writers, ForumX_moderators
UserA has the following groups assigned: ForumX_readers, ForumY_writers, ForumZ_moderators
But the problem is, since UserA belongs to groups that have publish_replies and moderate capabilities, he has full access to ForumX.
So what I need is an intersection of the forum-groups and the user-groups - which in this example is ForumX_readers.
The promising part, but...
I digged into the code of the plugin and found the line that handles the capabilities of the user based on his assigned groups and quickly tried to get the current forum groups, to implement the intersection.
Unfortunatelly I was not able to access the global $post, the $_GLOBALS['post'] nor the $_REQUEST[] variables in this part of code. Neither directly nor with an apply_filters() function, that I implemented into the part of the code myself.
UPDATE:
I was able to get the ID with get_posts() and the slug of the current forum/topic.
So, my question
Is there any solution to my first attempt, which I may have overseen?
If not, is there maybe any other plugin that can solve my problem that I'm not aware of?
Or is something like that even impossible in bbpress?
After some further research and trial & error, I finally figured it out.
First step to do is to set up the capabilities, which in my case look something like this.
In the plugins directory, there is the file core/class-groups-user.php. The init_cache() function retrieves the assigned groups to the user, and sets the according capabilities.
To not mess around to much with the core-plugin, I applied a filter to the $group_ids variable which can be found in line: 415.
foreach( $user_groups as $user_group ) {
$group_ids[] = Groups_Utility::id( $user_group->group_id );
}
// added this line
$group_ids = apply_filters('filter_user_group_ids', $group_ids);`
I then created a new plugin, which hooks into this filter.
add_filter('filter_user_group_ids', 'dnmc_filter_groups', 10, 1);
function dnmc_filter_groups($user_group_ids) {
$forum_id = dnmc_get_forum_id();
if(!$forum_id) return $user_group_ids;
$forum_group_ids = Groups_Post_Access::get_read_group_ids( $forum_id);
$user_restricted_forum_group_ids = array_intersect($user_group_ids, $forum_group_ids);
return $user_restricted_forum_group_ids;
}
function dnmc_get_forum_id() {
$args_topic = array(
'name' => basename( untrailingslashit( rtrim($_SERVER['REQUEST_URI'], '/') ) ),
'post_type' => 'topic',
'post_status' => 'publish',
'numberposts' => 1
);
if($topic = get_posts($args_topic)) {
return $topic[0]->post_parent;
}
$args_forum = array(
'name' => basename( untrailingslashit( rtrim($_SERVER['REQUEST_URI'], '/') ) ),
'post_type' => 'forum',
'post_status' => 'publish',
'numberposts' => 1
);
if($forum = get_posts($args_forum)) {
return $forum[0]->ID;
}
return false;
}

How to create Field Collection Item by code in Drupal 8

How to create a Field Collection item for a node by program in Drupal 8. I have tried with below code, but it doesn't work. 'field_abc_inside' is the field of the Field Collection 'field_abc'.
$field_collection_item = entity_create('field_collection_item', array(
'field_name' => 'field_abc',
'field_abc_inside' => array('value'=> 'Test data'),
));
$field_collection_item->setHostEntity($node);
$field_collection_item->save();
$user_id = \Drupal::currentUser()->getAccount()->id();
$user = User::load($user_id);
$fc = FieldCollectionItem::create(array(
"field_name" => "field_hobbies",
));
$fc->set('field_hobby_name', 'Watch TV');
$fc->setHostEntity($user);
That code helped me but I have one observation.
\Drupal::currentUser() is the user object, which is clear since it is using it to retrieve the id().
Therefore no need to User::load() it again in line 2, redundant processing that will cause the function to take longer to run.
// Get the current user object
$user = \Drupal::currentUser();
// Prepare the new Field Collection Item object
$fc = FieldCollectionItem::create(array(
"field_name" => "field_hobbies",
));
// Sets more field values
$fc->set('field_hobby_name', 'Watch TV');
// Sets the host record; where the field collection will be attached into
$fc->setHostEntity($user);
// Saves it into an actual field collection record
$fc->save();

Wordpress Loops: Excluding by Tag String, Register by Function?

I have 3 loops running on the front page.
One for the slider, and it wants posts that have the tag "slider".
One for featured articles, and it wants posts that have the tag "featured".
And a third I am calling in loop_latest.php, it wants the 6 latest posts; the trick here is those need to be the 6 latests that do NOT have the tags "slider" or "featured".
So I have this query.
$query = new WP_Query(
array(
'showposts' => '6',
'tag_not_in' => array('featured, slider'),
) );
I'm pretty sure the problem here is that "tag_not_in" doesn't like strings, it wants IDs.
SO! Is there a way to pass it the strings?
If not, how do I divine what the IDs of tags are?
Can I run some code in "functions.php" to set and reserve the ids? featured == 777, slider == 888
Something like that?
I'd like to be able to SET them so that gets packaged with this theme. Rather than have to hope that when the client registers the tags "slider" and "featured" they match up with what I have chosen in the loops/query.
the correct parameter is tag__not_in (with 2 underscores).
additionally - to get your tag ID put this code in your functions.php
function get_tag_id_by_name($tag_name) {
global $wpdb;
$tag_ID = $wpdb->get_var("SELECT * FROM ".$wpdb->terms." WHERE `name` = '".$tag_name."'");
return $tag_ID;
}
and your new code should be:
$featuredtag = get_tag_id_by_name('featured');
$slidertag = get_tag_id_by_name('slider');
$query = new WP_Query(
array(
'showposts' => '6',
'tag__not_in' => array($featuredtag, $slidertag),
) );

Change fields in node from code

How do I access the fields in a node in Drupal 7.
I have tried this but that do not work.
$node=node_load($nid);
$node->field_num[LANGUAGE_NONE][0]['value']=$num;
I think I have to be more specific:
I first create a node and set values on some fields like this:
$values = array(
'type' => 'scorings',
'uid' => $user->uid,
'status' => 1,
'comment' => 0,
'promote' => 0,
);
$entity = entity_create('node', $values);
$ewrapper = entity_metadata_wrapper('node', $entity);
$entity->field_rond_nid[LANGUAGE_NONE][0]['value']=$nid_scorekort;
$entity->field_golfid[LANGUAGE_NONE][0]['value']=$form_state['values']['golfid_1'];
$ewrapper->save(true);
entity_save('node', $entity);
$nid=$entity->nid;
This works fine. Then I want to access this node from another function (passing the nid to it) end set value to another field (field_score_1). I have tried this:
$node=node_load($nid, 'my_content type');
$node->field_score_1[LANGUAGE_NONE][]['value'] = $my_value;
But this do not work. Seams that node_load do not give me access to the fields.
Your node_load() call is incorrect.
From the Drupal API documentation for node_load() the function declaration is:
node.module node_load($nid = NULL, $vid = NULL, $reset = FALSE)
Your second parameter is a string, not a number, as $vid would be.
If node_load() returns a FALSE then it failed.
Perhaps you were wanting to use EntityFieldQuery() instead?

how to populate text area with database value before form load in drupal 7?

I have used hook_form_FORM_ID_alter( ) function to alter a menu_edit_menu form to add few fields. I ve stored the values from those fields in a table when submit action is performed. what I need now is when the particular form entry is loaded again for editing. The fields should be populated with the data in the database before form loads. Can anyone tell me how this could be done.
I have tried using hook_validate ( ) function but the function is called when the submit action is performed which is of no use in this regard. I have to perform the database query before the form gets rendered to populate the text fields with data from tables. Kindly provide me some insight into how this could be accomplished.
I have a problem with the sql select query as well/
$query = db_select('menu_custom');
$query
->fields(array('menu_name','role1','role2','role3'))
->condition('check',1,'=')
->conditon('title',$form_state['values']['title'])
->execute();
foreach($query as $record)
{
$mname = $result ->menu_name;
$rl1 = $result -> role1;
$rl2 = $result -> role2;
$rl3 = $result -> role3;
dpm($mname." ".$rl1);
}
I am getting error that my field specification is wrong but I cant figure out the problem there.
This is a bit too long for a comment so I'll put it here:
The only error you've got in your query is that the first argument passed to the fields() function needs to be the name/alias of the table from which the fields are coming. So your query should probably look something like this:
$query = db_select('menu_custom')
->fields('menu_custom', array('menu_name','role1','role2','role3'))
->condition('check',1,'=')
->conditon('title',$form_state['values']['title'])
->execute();
You get the data from your database in your form function, and put them as default_value
$name = db_query(YOUR_SQL);
$form['first_name'] = array(
'#title' => t('First Name'),
'#type' => 'textfield',
'#default_value' => $name,
);
is this what you meant?

Resources