Browse Source

allowed for view lookup in field definition

main
astanley 4 months ago
parent
commit
b944074de2
  1. 39
      src/NodeBuilder.php

39
src/NodeBuilder.php

@ -9,6 +9,7 @@ use Drupal\node\Entity\Node;
use Drupal\taxonomy\Entity\Term; use Drupal\taxonomy\Entity\Term;
use Drupal\Core\Config\ConfigFactory; use Drupal\Core\Config\ConfigFactory;
use Drupal\taxonomy\Entity\Vocabulary; use Drupal\taxonomy\Entity\Vocabulary;
use Drupal\views\Views;
/** /**
* Class to construct nodes from crossref DOI harvest. * Class to construct nodes from crossref DOI harvest.
@ -157,31 +158,49 @@ final class NodeBuilder {
} }
/** /**
* Get allowed vocabularies for a taxonomy term reference field. * Get allowed taxonomy vocabularies for a taxonomy reference field.
* *
* @param string $entity_type * @param string $entity_type
* The entity type (e.g., 'node'). * The entity type (e.g., 'node').
* @param string $bundle * @param string $bundle
* The bundle (content type machine name). * The bundle (e.g., 'article').
* @param string $field_name * @param string $field_name
* The machine name of the taxonomy term reference field. * The field machine name (e.g., 'field_tags').
* *
* @return string[] * @return array
* Array of allowed vocabulary IDs. * An array of allowed vocabulary IDs (vids).
*/ */
function get_allowed_vocabularies($entity_type, $bundle, $field_name) { public function get_allowed_vocabularies($entity_type, $bundle, $field_name) {
$config = $this->config->get('doi_prefill.settings');
$field_config = $this->entityTypeManager $field_config = $this->entityTypeManager
->getStorage('field_config') ->getStorage('field_config')
->load($entity_type . '.' . $bundle . '.' . $field_name); ->load($entity_type . '.' . $bundle . '.' . $field_name);
if ($field_config) { if (!$field_config) {
$settings = $field_config->getSettings(); return [];
if (isset($settings['handler_settings']['target_bundles'])) { }
$settings = $field_config->getSettings();
$handler = $settings['handler'];
if (strpos($handler, 'default:') === 0) {
if (!empty($settings['handler_settings']['target_bundles'])) {
return array_keys($settings['handler_settings']['target_bundles']); return array_keys($settings['handler_settings']['target_bundles']);
} }
} }
if ($handler === 'views' && !empty($settings['handler_settings']['view']['view_name'])) {
$view = Views::getView($settings['handler_settings']['view']['view_name']);
if ($view) {
$display = $settings['handler_settings']['view']['display_name'];
$view->setDisplay($display);
$filters = $view->getDisplay()->getOption('filters');
if (!empty($filters['vid']['value'])) {
return $filters['vid']['value']; // array of vids
}
}
}
return []; return [];
} }

Loading…
Cancel
Save