Drupal StackExchange

programmatically get child nodes referencing parent by entity reference uuid, using loadByProperties & entityTypeManager or other way without views

4 days 5 hours ago

I tried this answer:

https://drupal.stackexchange.com/a/298084/1082

But I can't seem to use

'field_scientists' => $entity_reference_uuid

where, in my case, field_scientists is a field of type Entity Reference UUID - which is a field type provided by https://www.drupal.org/project/entity_reference_uuid module

What happens for me is that I get nothing back, my results array from that example is empty.

Looking to do this programmatically in my own custom module, rather than use a Drupal View.

If there's another way e.g. that uses the ->query method for querying the database I'd be happy to use that programmatic approach.

Thank you.

Update

An example of a uuid of a node in my case is BMSSYS0000051988.

This was set in a migration import from a field in a CSV.

My attempts are:

1. based on the example in the other question: https://drupal.stackexchange.com/a/298084/1082

$nodes = \Drupal::entityTypeManager()->getStorage('node')->loadByProperties([ 'type' => 'bol', 'field_parent_uuid' => $taxonkey, ]);

2. db query

$query = \Drupal::entityQuery('node') ->condition('status', NODE_PUBLISHED) ->condition('type', 'bol'); $and = $query->andConditionGroup(); $and->condition('field_parent_uuid', $taxonkey); $query->condition($and); $result = $query->execute();

where $taxonkey is BMSSYS0000051988

both 1 and 2 attempts above return an empty array.

Update 2

here is my migration config that shows uuid being written to from a field in the csv. Is this legal? Should a developer's code change the uuid value or should uuid only be handled by Drupal core?

dependencies: { } id: bol_csv_import class: null field_plugin_method: null cck_plugin_method: null migration_tags: null migration_group: defaultl: publicbins label: 'Import bol species data' source: plugin: csv path: ./data/taxonomy_species_with_unpacked_bold.csv delimiter: ',' enclosure: '"' header_offset: 0 ids: - uuid fields: - name: uuidl: publicbins label: 'NBN id' - name: parent_uuid label: 'NBN id of parent' - name: name label: Name - name: rank label: 'Taxonomic Rank' - name: taxon_key label: 'Taxon Key' - name: authority label: authority - name: specimenrecords label: specimenrecords - name: publicrecords label: publicrecords - name: publicbins label: publicbins - name: taxid label: 'NBN Taxon ID' process: title: name uid: plugin: default_value default_value: 1 uuid: uuid field_parent_uuid: parent_uuid field_taxon_key: taxon_key field_authority: authority field_rank: rank field_num_bold_barcode_specimens: specimenrecords field_num_bold_public_records: publicrecords field_num_bold_public_bins: publicbins field_bold_taxon_id: taxid type: plugin: default_value default_value: bol destination: plugin: 'entity:node' migration_dependencies: null

Update 3

I'll write a db query to solve this issue. Running a command line sql indicates this approach could work:

MariaDB [db]> select * from node where nid = 246; +-----+------+------+------------------+----------+ | nid | vid | type | uuid | langcode | +-----+------+------+------------------+----------+ | 246 | 248 | bol | NBNORG0000041189 | en | +-----+------+------+------------------+----------+ 1 row in set (0.001 sec)
therobyouknow

Setting exposed filter default value from query string

4 days 6 hours ago

I have a view which show products and an exposed filter block (category, manufacturer, price).

I want to set category default value from URL, like this:

/browse-products/category-id

though, I wrote this module which take the parameter from URL and put it into exposed filter default value.

function mymodule_form_alter(&$form, &$form_state, $form_id) { global $request; if ($form_id == 'views_exposed_form') { $category_id = (int)($request->attributes->get('_raw_variables')->get('arg_0')); $form['category_id']['#default_value'][$category_id] = $category_id; dpm($form['category_id']); } }

dpm output for /browse-products/5

Array ( [#type] => checkboxes [#multiple] => 1 [#options] => Array ( [5] => Tablets [10] => -Ainovo [7] => -Galaxy Tab [6] => -IPad [1] => Smart Phones [4] => -Galaxy [3] => -IPhone [9] => --IPhone C [8] => --IPhone X ) [#size] => 9 [#default_value] => Array ( [5] => 5 ) [#bef_nested] => 1 [#theme] => bef_checkboxes )

but it doesn't work?

another thing, why dpm($form) prints the views exposed form data twice!

update 1: I tried something like this (but didn't work):

function mymodule_views_pre_build(Drupal\views\ViewExecutable $view) { $view_filters = $view->display_handler->getOption('filters'); $view_filters['field_category_target_id']['value'][8] = 8; $view->display_handler->setOption('filters', $view_filters); }

or this:

function mymodule_views_pre_build(Drupal\views\ViewExecutable $view) { $filters = $view->getExposedInput(); $filters['field_category_target_id'] = 8; $view->setExposedInput($filters); }
Wisamx

Limit row items to 1 as per the GROUP BY result in Views 3.x

4 days 7 hours ago

I'm trying to limit row items to 1 as per the GROUP BY result in Views 3.x, below is the view which I've created but I'm missing the key part from that, below is the exported code of my view.

$view = new view(); $view->name = 'position'; $view->description = ''; $view->tag = 'default'; $view->base_table = 'node'; $view->human_name = 'Position'; $view->core = 7; $view->api_version = '3.0'; $view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */ /* Display: Master */ $handler = $view->new_display('default', 'Master', 'default'); $handler->display->display_options['title'] = 'Position'; $handler->display->display_options['use_more_always'] = FALSE; $handler->display->display_options['access']['type'] = 'perm'; $handler->display->display_options['cache']['type'] = 'none'; $handler->display->display_options['query']['type'] = 'views_query'; $handler->display->display_options['exposed_form']['type'] = 'basic'; $handler->display->display_options['pager']['type'] = 'none'; $handler->display->display_options['pager']['options']['offset'] = '0'; $handler->display->display_options['style_plugin'] = 'table'; $handler->display->display_options['style_options']['grouping'] = array( 0 => array( 'field' => 'field_home_page_anchor_position', 'rendered' => 1, 'rendered_strip' => 0, ), ); $handler->display->display_options['style_options']['columns'] = array( 'title' => 'title', 'field_home_page_anchor_position' => 'field_home_page_anchor_position', ); $handler->display->display_options['style_options']['default'] = '-1'; $handler->display->display_options['style_options']['info'] = array( 'title' => array( 'sortable' => 0, 'default_sort_order' => 'asc', 'align' => '', 'separator' => '', 'empty_column' => 0, ), 'field_home_page_anchor_position' => array( 'sortable' => 0, 'default_sort_order' => 'asc', 'align' => '', 'separator' => '', 'empty_column' => 0, ), ); /* Field: Content: Title */ $handler->display->display_options['fields']['title']['id'] = 'title'; $handler->display->display_options['fields']['title']['table'] = 'node'; $handler->display->display_options['fields']['title']['field'] = 'title'; $handler->display->display_options['fields']['title']['label'] = ''; $handler->display->display_options['fields']['title']['alter']['word_boundary'] = FALSE; $handler->display->display_options['fields']['title']['alter']['ellipsis'] = FALSE; /* Field: Content: Home Page Anchor Position */ $handler->display->display_options['fields']['field_home_page_anchor_position']['id'] = 'field_home_page_anchor_position'; $handler->display->display_options['fields']['field_home_page_anchor_position']['table'] = 'field_data_field_home_page_anchor_position'; $handler->display->display_options['fields']['field_home_page_anchor_position']['field'] = 'field_home_page_anchor_position'; /* Sort criterion: Content: Post date */ $handler->display->display_options['sorts']['created']['id'] = 'created'; $handler->display->display_options['sorts']['created']['table'] = 'node'; $handler->display->display_options['sorts']['created']['field'] = 'created'; $handler->display->display_options['sorts']['created']['order'] = 'DESC'; /* Sort criterion: Content: Home Page Anchor Position (field_home_page_anchor_position) */ $handler->display->display_options['sorts']['field_home_page_anchor_position_value']['id'] = 'field_home_page_anchor_position_value'; $handler->display->display_options['sorts']['field_home_page_anchor_position_value']['table'] = 'field_data_field_home_page_anchor_position'; $handler->display->display_options['sorts']['field_home_page_anchor_position_value']['field'] = 'field_home_page_anchor_position_value'; /* Filter criterion: Content: Published */ $handler->display->display_options['filters']['status']['id'] = 'status'; $handler->display->display_options['filters']['status']['table'] = 'node'; $handler->display->display_options['filters']['status']['field'] = 'status'; $handler->display->display_options['filters']['status']['value'] = 1; $handler->display->display_options['filters']['status']['group'] = 1; $handler->display->display_options['filters']['status']['expose']['operator'] = FALSE; /* Filter criterion: Content: Type */ $handler->display->display_options['filters']['type']['id'] = 'type'; $handler->display->display_options['filters']['type']['table'] = 'node'; $handler->display->display_options['filters']['type']['field'] = 'type'; $handler->display->display_options['filters']['type']['value'] = array( 'news_story' => 'news_story', ); /* Filter criterion: Content: Home Page Anchor Position (field_home_page_anchor_position) */ $handler->display->display_options['filters']['field_home_page_anchor_position_value']['id'] = 'field_home_page_anchor_position_value'; $handler->display->display_options['filters']['field_home_page_anchor_position_value']['table'] = 'field_data_field_home_page_anchor_position'; $handler->display->display_options['filters']['field_home_page_anchor_position_value']['field'] = 'field_home_page_anchor_position_value'; $handler->display->display_options['filters']['field_home_page_anchor_position_value']['operator'] = 'not empty'; $handler->display->display_options['filters']['field_home_page_anchor_position_value']['value'] = array( 'min' => '', 'max' => '', 'value' => '', ); /* Display: Block */ $handler = $view->new_display('block', 'Block', 'block'); $translatables['position'] = array( t('Master'), t('Position'), t('more'), t('Apply'), t('Reset'), t('Sort by'), t('Asc'), t('Desc'), t('Home Page Anchor Position'), t('Block'), );

SQL query generated by View is:

SELECT node.title AS node_title, node.nid AS nid, node.created AS node_created, field_data_field_home_page_anchor_position.field_home_page_anchor_position_value AS field_data_field_home_page_anchor_position_field_home_page_a, 'node' AS field_data_field_home_page_anchor_position_node_entity_type FROM {node} node LEFT JOIN {field_data_field_home_page_anchor_position} field_data_field_home_page_anchor_position ON node.nid = field_data_field_home_page_anchor_position.entity_id AND (field_data_field_home_page_anchor_position.entity_type = 'node' AND field_data_field_home_page_anchor_position.deleted = '0') WHERE (( (node.status = '1') AND (node.type IN ('news_story')) AND (field_data_field_home_page_anchor_position.field_home_page_anchor_position_value IS NOT NULL ) )) ORDER BY node_created DESC, field_data_field_home_page_anchor_position_field_home_page_a ASC
Rishi Kulshreshtha

How can I set view permissions for content type to make it visible to autenticated users only?

4 days 9 hours ago

I want a content type to be only visible to authenticated users.

Now the edit permission dialog only offers permissions for modification, not for displaying the published content.

Until Drupal 9, I've used the Permissions by Term module to limit public access for certain page areas and content types. Since Permissions by Term is not yet Drupal 10 compatible I had to remove it.

Is there an elegant, ideally Drupal 10 native way, to limit certain content types from public access?

david

hreflang links added to page source without translation

4 days 9 hours ago

When Interface Translation module is enabled in Drupal 8, I get hreflang links to ALL languages on the site for every page view sourse, no matter if the content is translated or not.

If alternate hrefland is active on all of the pages, even untranslated ones, it creates some SEO problems.

I want to remove these links. Is any Drupal way to do this?

latikas

Is there a function to get the system path for the current language?

4 days 10 hours ago

If I'm using multiple languages on the site with path prefixing, is there a quick way to get a system path for the current language?

For example, if the current language is English, and I'm interested getting the English path for 'user/password', what function call (or small number of lines) will return 'en/user/password'?

The following doesn't seem to work; it returns nothing:

drupal_lookup_path('alias', 'user/password', $language->language);
colan

Update the block through ajax

4 days 11 hours ago

I have added a block which shows the no of items added to an entity. I want to refresh the block through ajax as the items are added to an entity through ajax form so whenever I add an item, it will refresh or re-render the block content too.

I found some of the modules, but they are in Drupal 7 and not available for D8 so I want to make it done with my custom module but didn't find anything useful.

UPDATED

I have added the class attribute with the block and whenever I add any items, I made an ajax request through javascript which calls the respective controller and at the same time, I run this too--

// This is a part of the controller function from where I am calling below defined methods. $this->updateBlock(); protected function getBlock() { $block = $this->blockManager->createInstance('block_id', []); return $block; } public function updateBlock() { $block = $this->getBlock(); $this->response->addCommand(new ReplaceCommand('.block--class', $block->build())); return $this->response;

}

But It is not updating the block.

Ankit Jain

"File not found error" when installing using composer

4 days 13 hours ago

I'm running Docker on Ubuntu 18 with a container for MariaDB, Nginx and php-fpm (7.3).
The PHP container has composer installed, and I used it to create a Drupal Commerce project. I can start the Drupal install process using a browser, but after I input the DB settings, the process fails and I get the following error.

An AJAX HTTP error occurred.
HTTP Result Code: 404
Debugging information follows.
Path: /core/install.php?profile=commerce_base&langcode=en&id=1&op=do_nojs&op=do
StatusText: Not Found
ResponseText: File not found.

Increasing the execution time, post time, memory limit, post and upload sizes in the php.ini file, as recommended by another user, didn't solve the problem.

Nginx error log contains the following message.

[error] 6#6: *46 FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream'.

The database has tables and data in it, so I know the database credentials are correct.

What am I doing wrong?

Robert