I'm using the fantastic WPGraphQL plugin and also the WPGraphQL for ACF plugin.
So far I have been able to query by an ACF custom field of type String, but now I want to query by another field (also String) inside a repeater field.
When I add the repeater field using the regular functions to register the field and the where clause, the GraphiQL IDE page doesn't load, so there's probably an Internal Server Error.
Here is my code. I have tried both "Repeater" and "repeater" (lower case) as the type.
register_graphql_field('Post', 'matchedProducts', [
'description' => 'The matched products of the post object',
'type' => 'repeater',
'resolve' => function(\WPGraphQL\Model\Post $post) {
return get_post_meta($post->databaseId, 'matched_products', true);
}
]);
register_graphql_field('RootQueryToPostConnectionWhereArgs', 'matchedProducts', [
'type' => 'repeater',
'description' => 'Query posts by matched products',
]);
Related
I add multiple fields with
$view->addHandler($view->current_display, 'field', 'views', 'nothing', array(
'label' => 'My field',
'type' => 'textfield',
'alter' => array('text' => 'My field text'),
'element_class' => 'my-field',
'element_default_classes' => 0,
'group_rows' => TRUE
), 'my_field');
this works fine but i can't find a way to alter it, because the result data in pre_render, post_render, post_execute doesn't include these field only has entity fields which can be changed with
$result->_entity->set('title', 'newtitle')
i managed to change the field values eventually with template_preprocess_views_view_field() but that doesn't work for data export .csv / .xls
created a new views field in hook_views_data and a new plugin extending FieldPluginBase but that that doesn't work with addHandler.
is there a way to add dynamic fields and modify the output or create a new viewsfield and use it with $view->addHandler ?
have to create a new views field plugin and use that instead of nothing, and in the render function the value can be customized
Is there a way to create a formatter for drupal email field to decrypt when viewed in a view table.
I have tried to create a formatter for the same using the below code
function MYMODULE_field_formatter_info() {
return array(
'views_decrypt_field' => array(
'label' => t('Decrypt this field'),
'field types' => array('textfield'),
),
);
}
function MYMODULE_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
$settings = $display['settings'];
$element = array();
if ($display['type'] == 'views_decrypt_field') {
dsm($items);
}
return $element;
}
when I run this code, other fields with "textfield" type shows this formatter.
I am trying to create a generic formatter for all "text" type fields so that if they are encrypted then I can use this formatter in the view to decrypt the same.
N.B: The email field is not showing any formatter dropdown in the field settings in the VIEW.
You need to use all function listing in this page : https://api.drupal.org/api/drupal/modules!field!field.api.php/group/field_formatter/7.x
hook_field_formatter_info => Expose Field API formatter types.
hook_field_formatter_info_alter => Perform alterations on Field API formatter types.
hook_field_formatter_prepare_view => Allow formatters to load information for field values being displayed.
hook_field_formatter_view => Build a renderable array for a field value.
I don't know if i am asking this correctly but plz help me.
I am using Drupal Views to display RSS for my site. I am using the 'Views RSS: Core Elements' module to map the RSS fields with the custom fields added by me in the views. All works well. But a requirement forces me to add custom item elements in the RSS display. I am implementing the hook_views_rss_item_elements() to add custom item elements. When i view the RSS page i get the see the custom items but when i try to read the RSS using a RSS reader, the items are not being read. What am i doing wrong, or am i missing something?
Here is my code to add custom items to the 'SHOW ITEM ELEMENTS : CORE' for RSS display:
function Mymodule_views_rss_item_elements() {
$elements['blogs_title_image'] = array ( 'title' => t('Blogs Title Image'), 'description' => t('Blogs Title Image'), );
$elements['blogs_additional_tags'] = array ( 'title' => t('Blogs Additional Tags'), 'description' => t('Blogs Additional Tags'), );
$elements['blogs_short_desc'] = array ( 'title' => t('Blogs Short Description'), 'description' => t('Blogs Short Description'), );
return $elements;
}
After some research i did manage to find my way out:
I implemented hook_views_rss_namespaces to add a custom namespace and the custom items under the namespace:
function Mymodule_views_rss_namespaces() {
$namespaces['blogs'] = array(
'prefix' => 'xmlns',
'uri' => 'http://base.google.com/ns/1.0',
);
return $namespaces;
}
Note: Use the same 'prefix' and 'uri' as given in the above code or else namespace uri error will arise.
I created a custom field with the "advanced-custom-fields"-plugin. Now I want to get and output the custom field programmatically in my template file (backend, edit page), because my template is called via ajax if user want's to add a new region to the page.
Is there any function which returns the complete field? I only found functions which gave me values, but not the field as "form".
I found a solution. I duplicated the plugin folder into my theme root directory and put the following code into my functions.php:
function relationshipField() {
$newField = new acf_field_relationship();
$field = array(
'post_type' => array('post'),
'max' => '',
'taxonomy' => array('all'),
'filters' => array('search'),
'result_elements' => array('post_title', 'post_type'),
'return_format' => 'object'
);
return $newField->create_field($field);
}
In addition I append a custom input field (created in function.php) which stores only the post id's in database.
I'm writing a plugin which creates a custom post_type called "dictionary_entry" which has several custom meta boxes and fields. I'd like to add an addition field which allows the custom post author to upload an audio clip.
I've done some digging and tried the code offered here but I can't get it to work.
I think one possible answer to my question would be the "type" parameter for fields. I've seen "text", "textarea", "time", "color", "radio", etc. but I haven't been able to find a list of all the possibilities. Is it wishful thinking that there might be a field type: "file" or "upload"?
I'm going to skip the code for adding the custom post_type, but here's an example of my code for adding the meta boxes (in case somebody else is trying to use this, remember to use your custom post_type in the 'pages' parameter):
//meta box code
$meta_boxes = array();
$meta_boxes[] = array(
'id' => 'examples', // meta box id, unique per meta box
'title' => 'Examples', // meta box title
'pages' => array('dictionary_entry'), // post types, accept custom post types as well, default is array('post'); optional
'context' => 'normal', // where the meta box appear: normal (default), advanced, side; optional
'priority' => 'high', // order of meta box: high (default), low; optional
'fields' => array( // list of meta fields
array(
'name' => 'Example 1', // field name
'desc' => 'Use it in a sentence? EX: Kanien\'kéha kahrónkha.', // field description, optional
'id' => $prefix . 'example1', // field id, i.e. the meta key
'type' => 'text', // text box
'std' => '', // default value, optional
'validate_func' => 'check_apos' // validate function, created below, inside RW_Meta_Box_Validate class
),
array(
'name' => 'Translation 1', // field name
'desc' => 'What does the sentence mean? EX: I speak Mohawk.', // field description, optional
'id' => $prefix . 'ex_translation1', // field id, i.e. the meta key
'type' => 'text', // text box
'std' => '', // default value, optional
'validate_func' => 'check_apos' // validate function, created below, inside RW_Meta_Box_Validate class
)
)
);
foreach ($meta_boxes as $metabox) {
add_meta_box... //see the codex for add_meta_box()
}
I figured out how to do this by digging into the code found here. If you take a look, you'll recognize parts of my code quoted above. I was originally using this class, but didn't fully realize it. It's a custom class which can be called to add various meta boxes / fields.
It turns out that the "type" parameter I was wondering about actually belongs to this Class (as opposed to the Wordpress API) and that it does allow for a type: 'file' which brings up a default file picker window (not the built-in media uploader). For my purposes this is okay, because I don't need all the slick options.
If you're reading this, you've probably already googled this question and seen a wide variety of posts that partially explain how to do this. For what it's worth, I found this to be the easiest way to add this functionality that ALSO works for custom post_types (without a fair amount of hacking). I hope this is useful to someone else.