From fcb1251de9cc51e8c1d867673814310eb8612c48 Mon Sep 17 00:00:00 2001 From: Eric Bremner <ebremner@uwaterloo.ca> Date: Tue, 9 Nov 2021 17:14:44 +0000 Subject: [PATCH] ISTWCMS-5195: cleaning up code for better readability --- src/Service/UwNodeFieldValue.php | 213 ++++++++++++++++++++++--------- 1 file changed, 152 insertions(+), 61 deletions(-) diff --git a/src/Service/UwNodeFieldValue.php b/src/Service/UwNodeFieldValue.php index 4896c863..011023a8 100644 --- a/src/Service/UwNodeFieldValue.php +++ b/src/Service/UwNodeFieldValue.php @@ -65,11 +65,7 @@ class UwNodeFieldValue { // Address field type. if ($type == 'address') { - $address = $node->$field_name->getValue(); - if (isset($address[0])) { - return $address[0]; - } - return NULL; + return $this->getAddressField($node, $field_name); } // Author field type. @@ -84,14 +80,7 @@ class UwNodeFieldValue { // Only content, either layout builder or summary. if ($type == 'content') { - if ($view_mode == 'teaser') { - return [ - '#type' => 'processed_text', - '#text' => $node->$field_name->value, - '#format' => $node->$field_name->format, - ]; - } - return NULL; + return $this->getContentField($node, $view_mode, $field_name); } // Office hours field type. @@ -106,12 +95,7 @@ class UwNodeFieldValue { // Taxonomy terms field type. if ($type == 'terms') { - $tags = []; - foreach ($field_name as $field) { - $tags_to_add = $this->getTermsFromEntityField($node->$field, 'tags'); - $tags = array_merge($tags, $this->getTermsFromEntityField($node->$field, 'tags')); - } - return $tags; + return $this->getTermsField($node, $field_name); } // Image field type. @@ -126,20 +110,7 @@ class UwNodeFieldValue { // Map (leaflet) field type. if ($type == 'map') { - // Set the map initially to null, if there are - // coordinates, then will be replaced. - $map = NULL; - - // If there are coordinates, set the map. - if ($node->$field_name->getValue()) { - $display = [ - 'type' => 'leaflet_formatter_default', - 'label' => 'visually_hidden', - ]; - $map = $node->$field_name->view($display); - } - - return $map; + return $this->getMapField($node, $field_name); } // Plain text field type (textbox). @@ -155,48 +126,168 @@ class UwNodeFieldValue { // Catalog tags, this is a special type of terms, since we // need to worry about the tabs that go along with them. if ($type == 'catalog_tags') { + return $this->getCatalogTags($node, $field_name); + } - // Empty arrays so that we don't get undefined - // index errors. - $tabs = []; - $tags = []; + // Title of the node. + if ($type == 'title') { + return $node->getTitle(); + } - // Get the entity and values for the catalog, - // which is the taxonomy term catalog. - $catalog_entity = $node->field_uw_catalog_catalog->entity; - $tabs_values = $catalog_entity->field_uw_catalog_tabs_display->getValue(); + // URL of the node. + if ($type == 'url') { + return $node->toUrl()->toString(); + } + } - // Setup the array that we can use for in_array, - // which is the tabs to be displayed. - foreach ($tabs_values as $tab_value) { - $tabs[] = $tab_value['value']; - } + /** + * Function to get catalog tags. + * + * @param \Drupal\node\Entity\Node $node + * The node. + * @param string $field_name + * The field name. + * + * @return array + * Array of values for the field. + * + * @throws \Drupal\Core\Entity\EntityMalformedException + */ + public function getCatalogTags(Node $node, string $field_name): array { + + // Empty arrays so that we don't get undefined + // index errors. + $tabs = []; + $tags = []; + + // Get the entity and values for the catalog, + // which is the taxonomy term catalog. + $catalog_entity = $node->field_uw_catalog_catalog->entity; + $tabs_values = $catalog_entity->field_uw_catalog_tabs_display->getValue(); + + // Setup the array that we can use for in_array, + // which is the tabs to be displayed. + foreach ($tabs_values as $tab_value) { + $tabs[] = $tab_value['value']; + } - // If there are tabs, then get them. - if (!empty($tabs)) { - foreach ($field_name as $key => $field) { - if (in_array($key, $tabs)) { - $tags_to_add = $this->getTermsFromEntityField($node->$field, 'tags'); + // If there are tabs, then get them. + if (!empty($tabs)) { + foreach ($field_name as $key => $field) { + if (in_array($key, $tabs)) { + $tags_to_add = $this->getTermsFromEntityField($node->$field, 'tags'); - if (!empty($tags_to_add)) { - $tags[$key] = $this->getTermsFromEntityField($node->$field, 'tags'); - } + if (!empty($tags_to_add)) { + $tags[$key] = $this->getTermsFromEntityField($node->$field, 'tags'); } } } + } + + return $tags; + } - return $tags; + /** + * Get the value of a map field. + * + * @param \Drupal\node\Entity\Node $node + * The node. + * @param string $field_name + * The field name. + * + * @return mixed + * Array of field value or NULL. + */ + public function getMapField(Node $node, string $field_name) { + + // Set the map initially to null, if there are + // coordinates, then will be replaced. + $map = NULL; + + // If there are coordinates, set the map. + if ($node->$field_name->getValue()) { + $display = [ + 'type' => 'leaflet_formatter_default', + 'label' => 'visually_hidden', + ]; + $map = $node->$field_name->view($display); } - // Title of the node. - if ($type == 'title') { - return $node->getTitle(); + return $map; + } + + /** + * Get the value of an content field. + * + * @param \Drupal\node\Entity\Node $node + * The node. + * @param string $view_mode + * The view mode. + * @param string $field_name + * The field name. + * + * @return mixed + * Array of field value or NULL. + */ + public function getContentField(Node $node, string $view_mode, string $field_name) { + + if ($view_mode == 'teaser') { + return [ + '#type' => 'processed_text', + '#text' => $node->$field_name->value, + '#format' => $node->$field_name->format, + ]; } - // URL of the node. - if ($type == 'url') { - return $node->toUrl()->toString(); + return NULL; + } + + /** + * Get the taxonomy terms field(s) value. + * + * @param \Drupal\node\Entity\Node $node + * The node. + * @param array $field_name + * Array of field names. + * + * @return array + * Array of taxomoy term values. + * + * @throws \Drupal\Core\Entity\EntityMalformedException + */ + public function getTermsField(Node $node, array $field_name): array { + + // Need empty array in case there are no terms. + $tags = []; + + // Step through each of the terms and add to array. + foreach ($field_name as $field) { + $tags = array_merge($tags, $this->getTermsFromEntityField($node->$field, 'tags')); } + + // Return array of terms. + return $tags; + } + + /** + * Get the value of an address field. + * + * @param \Drupal\node\Entity\Node $node + * The node. + * @param string $field_name + * The field name. + * + * @return mixed + * Array of field value or NULL. + */ + public function getAddressField(Node $node, string $field_name) { + + $address = $node->$field_name->getValue(); + if (isset($address[0])) { + return $address[0]; + } + + return NULL; } /** -- GitLab