Skip to content

Commit

Permalink
Merge pull request #51 from CU-CommunityApps/localist_tags
Browse files Browse the repository at this point in the history
Add functionality to process localist tags into event content type
  • Loading branch information
judaw authored Feb 26, 2024
2 parents a689c66 + f7c5627 commit 4921012
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/Entity/LocalistConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@
* "publish_events_bool",
* "pull_specified_departments",
* "extra_parameters",
* "localist_tags_taxonomy",
* "localist_tags_field_name",
* "localist_tags_use_existing",
* },
* links = {
* "edit-form" = "/admin/config/system/localist_pull/{localist_pull}",
Expand Down Expand Up @@ -230,4 +233,25 @@ class LocalistConfig extends ConfigEntityBase implements LocalistInterface {
*/
public $localist_event_type_field_name;

/**
* The localist_tags_field_name.
*
* @var string
*/
public $localist_tags_field_name;

/**
* The localist_tags_taxonomy.
*
* @var string
*/
public $localist_tags_taxonomy;

/**
* The localist_tags_use_existing.
*
* @var boolean
*/
public $localist_tags_use_existing;

}
31 changes: 31 additions & 0 deletions src/Form/LocalistEntityForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,37 @@ public function form(array $form, FormStateInterface $form_state) {
'#required' => FALSE,
];

$form['tags_label'] = array(
'#type' => 'label',
'#title' => $this->t('<h3>Localist Tags</h3>
A way to pull in tags from localist to a taxonomy field in Drupal.'),
);
$form['localist_tags_taxonomy'] = [
'#type' => 'textfield',
'#title' => $this->t('Taxonomy for tags from localist'),
'#default_value' => $localist_pull->localist_tags_taxonomy,
'#size' => 20,
'#maxlength' => 255,
'#description' => $this->t('Machine name of the taxonomy to feed localist tags.'),
'#required' => FALSE,
];
$form['localist_tags_field_name'] = [
'#type' => 'textfield',
'#title' => $this->t('Event CT field for Localist tags'),
'#default_value' => $localist_pull->localist_tags_field_name,
'#size' => 20,
'#maxlength' => 255,
'#description' => $this->t('Mapping: tags term reference field machine name.'),
'#required' => FALSE,
];

$form['localist_tags_use_existing'] = [
'#type' => 'checkbox',
'#title' => $this->t('Only use existing tags from Drupal for localist tags.'),
'#default_value' => $localist_pull->localist_tags_use_existing,
];


// You will need additional form elements for your custom properties.
return $form;
}
Expand Down
38 changes: 38 additions & 0 deletions src/LocalistProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ private function create_node_create_array() {
if (!empty($this->config->get('localist_event_type_field_name')) && $this->config->get('localist_event_type_field_name') != '') {
$node_create_array[$this->config->get('localist_event_type_field_name')] = '';
}
if (!empty($this->config->get('localist_tags_field_name')) && $this->config->get('localist_tags_field_name') != '') {
$node_create_array[$this->config->get('localist_tags_field_name')] = '';
}
return $node_create_array;
}

Expand Down Expand Up @@ -156,6 +159,19 @@ private function get_localist_event_data($event, $node_array) {
$new_array[$this->config->get('localist_event_type_field_name')] = $event_type_term_array;
}
break;

case $this->config->get('localist_tags_field_name'):
if (!empty($event['event']['tags']) && $event['event']['tags'] != '') {
$event_type_term_array = array();
foreach ($event['event']['tags'] as $localist_tag) {
$target_id = $this->find_or_create_localist_tag($localist_tag);
if($target_id) {
$event_type_term_array[] = ['target_id' => $target_id];
}
}
$new_array[$this->config->get('localist_tags_field_name')] = $event_type_term_array;
}
break;
}
}
$new_array['title'] = $event['event']['title'];
Expand Down Expand Up @@ -257,6 +273,28 @@ private function find_or_create_event_type($term_name) {
return array_shift($term)->id();
}

private function find_or_create_localist_tag($term_name) {
$tax_vid = $this->config->get('localist_tags_taxonomy');
$use_existing_only = $this->config->get('localist_tags_use_existing');

$term = null;
$term = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->loadByProperties(['name' => $term_name, 'vid' => $tax_vid]);

if (empty($term) || is_null($term)) {
if($use_existing_only) {
return null;
}
$new_term = Term::create([
'vid' => $tax_vid,
'name' => $term_name,
]);
$new_term->enforceIsNew();
$new_term->save();
$term = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->loadByProperties(['name' => $term_name, 'vid' => $tax_vid]);
}
return array_shift($term)->id();
}

protected function getTermByField($tax_search_field, $term_name) {
$query_string = "select taxonomy_term_field_data.tid";
$query_string .= " from taxonomy_term_field_data, taxonomy_term__" . $tax_search_field;
Expand Down

0 comments on commit 4921012

Please sign in to comment.