Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
U
uw_cfg_common
Manage
Activity
Members
Labels
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Model registry
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
WCMS
uw_cfg_common
Commits
fcb1251d
Commit
fcb1251d
authored
3 years ago
by
Eric Bremner
Browse files
Options
Downloads
Patches
Plain Diff
ISTWCMS-5195: cleaning up code for better readability
parent
d5cf81fe
No related branches found
Branches containing commit
No related tags found
Tags containing commit
1 merge request
!172
Feature/istwcms 5128 ebremner theme node services
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/Service/UwNodeFieldValue.php
+152
-61
152 additions, 61 deletions
src/Service/UwNodeFieldValue.php
with
152 additions
and
61 deletions
src/Service/UwNodeFieldValue.php
+
152
−
61
View file @
fcb1251d
...
...
@@ -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
;
}
/**
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment