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
59b8ebeb
Commit
59b8ebeb
authored
4 years ago
by
Eric Bremner
Browse files
Options
Downloads
Patches
Plain Diff
ISTWCMS-4027: adding getTeaserContent to UwService
parent
88a5dd8e
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/Service/UWService.php
+106
-0
106 additions, 0 deletions
src/Service/UWService.php
with
106 additions
and
0 deletions
src/Service/UWService.php
+
106
−
0
View file @
59b8ebeb
...
...
@@ -3,6 +3,8 @@
namespace
Drupal\uw_cfg_common\Service
;
use
Drupal\Core\Entity\EntityInterface
;
use
Drupal\node\Entity\Node
;
use
Drupal\node\NodeInterface
;
/**
* Class UWService
...
...
@@ -15,8 +17,10 @@ class UWService implements UWServiceInterface {
* {@inheritDoc}
*/
public
function
prepareResponsiveImage
(
EntityInterface
$entity
,
string
$image_style
):
array
{
// Load in the file object if we have one.
if
(
$file
=
$entity
->
field_media_image
->
entity
)
{
// Need to set these variables so that responsive image function,
// has all the necessary info to process the image style.
$variables
[
'uri'
]
=
$file
->
getFileUri
();
...
...
@@ -41,4 +45,106 @@ class UWService implements UWServiceInterface {
return
[];
}
/**
* {@inheritDoc}
*/
public
function
getTeaserContent
(
NodeInterface
$node
,
array
$variables_to_get
,
string
$teaser_type
):
array
{
// Step through each of the variables to get and set the variables for the teaser content.
foreach
(
$variables_to_get
as
$variable_to_get
)
{
// Switch on the variable to get and set the variables for the teaser content.
switch
(
$variable_to_get
)
{
case
'title'
:
// Set the title from the node object.
$variables
[
'title'
]
=
$node
->
getTitle
();
break
;
case
'url'
:
// Set the title from the node object.
$variables
[
'url'
]
=
$node
->
toUrl
()
->
toString
();
break
;
case
'date'
:
// Set the field name, all of our content types use the same format,
// field_uw_<content_type>_date.
$field_name
=
'field_uw_'
.
trim
(
$teaser_type
)
.
'_date'
;
// Set the date variable, once returned to the calling function, they can
// change the date format as required (i.e. change it to long-date or date-time).
$variables
[
'date'
]
=
$node
->
$field_name
->
getString
();
break
;
case
'author'
:
// If author is selected, get both the author name and link.
$variables
[
'author_name'
]
=
$node
->
getOwner
()
->
getDisplayName
();
$variables
[
'author_link'
]
=
$node
->
getOwner
()
->
toUrl
()
->
toString
();
break
;
case
'sources'
:
// Event has a different name for the image than the rest, so ensuring
// that we get the correct field name for the image.
// In most cases it is field_uw_<content_type>_lising_page_image.
if
(
$teaser_type
==
'event'
)
{
$field_name
=
'field_uw_event_listing_page_img'
;
}
else
{
$field_name
=
'field_uw_'
.
trim
(
$teaser_type
)
.
'_listing_page_image'
;
}
// Get the image object from the node.
$image
=
$node
->
$field_name
->
entity
;
// Get all the image variables, including the sources using the prepareResponsiveImage function.
$image_variables
=
$this
->
prepareResponsiveImage
(
$image
,
'uw_ris_media'
);
// Set the responsive image variables for the teaser content.
$variables
[
'sources'
]
=
$image_variables
[
'responsive_sources'
];
$variables
[
'img_element'
]
=
$image_variables
[
'img_element'
][
'#uri'
];
$variables
[
'alt'
]
=
$image
->
field_media_image
->
alt
;
break
;
case
'tags'
:
// Set the field name for the tags which is field_uw_<content_type>_tags'.
$field_name
=
'field_uw_'
.
trim
(
$teaser_type
)
.
'_tags'
;
// Get all the taxonomy terms for the blog.
// Step through each taxonomy term and get the tag info.
foreach
(
$node
->
$field_name
as
$tag
)
{
// Set the tags in the teaser content variables.
$variables
[
'tags'
][]
=
[
'title'
=>
$tag
->
entity
->
name
->
value
,
'url'
=>
$tag
->
entity
->
toUrl
()
->
toString
(),
];
}
break
;
case
'content'
:
// Set the field name for the summary, which is field_name<content_type>_summary.
$field_name
=
'field_uw_'
.
$teaser_type
.
'_summary'
;
// Set the render array for the summary, we simply can not just send the
// value as Drupal will just render it with no filters or HTML applied.
// We need to send the full render array.
$variables
[
'content'
]
=
[
'#type'
=>
'processed_text'
,
'#text'
=>
$node
->
$field_name
->
value
,
'#format'
=>
$node
->
$field_name
->
format
,
];
break
;
}
}
return
$variables
;
}
}
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