Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
drupalorg
Manage
Activity
Members
Labels
Plan
Wiki
Custom issue tracker
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Model registry
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
project
drupalorg
Merge requests
!316
Apply changes from MR312.
Code
Review changes
Check out branch
Download
Patches
Plain diff
Merged
Apply changes from MR312.
issue/drupalorg-3494493:3494493-sponsor-widget-code
into
1.0.x
Overview
0
Commits
1
Pipelines
2
Changes
3
Merged
Fran Garcia-Linares
requested to merge
issue/drupalorg-3494493:3494493-sponsor-widget-code
into
1.0.x
4 months ago
Overview
0
Commits
1
Pipelines
2
Changes
3
Expand
Closes
#3494493
0
0
Merge request reports
Compare
1.0.x
version 1
c1cd20d1
4 months ago
1.0.x (base)
and
latest version
latest version
c1cd20d1
1 commit,
4 months ago
version 1
c1cd20d1
1 commit,
4 months ago
3 files
+
202
−
0
Side-by-side
Compare changes
Side-by-side
Inline
Show whitespace changes
Show one file at a time
Files
3
Search (e.g. *.vue) (Ctrl+P)
src/Plugin/Block/DrupalOrgSponsorWidget.php
0 → 100644
+
173
−
0
Options
<?php
namespace
Drupal\drupalorg\Plugin\Block
;
use
Drupal\Core\Block\BlockBase
;
use
Drupal\Core\Cache\Cache
;
use
Drupal\Core\Plugin\ContainerFactoryPluginInterface
;
use
Symfony\Component\DependencyInjection\ContainerInterface
;
use
Drupal\Core\Routing\RouteMatchInterface
;
use
Drupal\node\NodeInterface
;
use
Drupal\Core\Url
;
use
Drupal\file\Entity\File
;
use
Drupal\image\Entity\ImageStyle
;
/**
* Provides a 'DrupalOrg Sponsor Widget' Block.
*
* @Block(
* id = "drupalorg_sponsor_widget",
* admin_label = @Translation("DrupalOrg Sponsor Widget"),
* )
*/
class
DrupalOrgSponsorWidget
extends
BlockBase
implements
ContainerFactoryPluginInterface
{
/**
* The current route match.
*
* @var \Drupal\Core\Routing\RouteMatchInterface
*/
protected
$routeMatch
;
/**
* Constructs a new DrupalOrgSponsorWidget instance.
*
* @param array $configuration
* A configuration array containing information about the plugin instance.
* @param string $plugin_id
* The plugin_id for the plugin instance.
* @param mixed $plugin_definition
* The plugin implementation definition.
* @param \Drupal\Core\Routing\RouteMatchInterface $route_match
* The current route match.
*/
public
function
__construct
(
array
$configuration
,
$plugin_id
,
$plugin_definition
,
RouteMatchInterface
$route_match
)
{
parent
::
__construct
(
$configuration
,
$plugin_id
,
$plugin_definition
);
$this
->
routeMatch
=
$route_match
;
}
/**
* {@inheritdoc}
*/
public
static
function
create
(
ContainerInterface
$container
,
array
$configuration
,
$plugin_id
,
$plugin_definition
)
{
return
new
static
(
$configuration
,
$plugin_id
,
$plugin_definition
,
$container
->
get
(
'current_route_match'
)
);
}
/**
* {@inheritdoc}
*/
public
function
build
()
{
/** @var \Drupal\node\NodeInterface $node */
$node
=
$this
->
routeMatch
->
getParameter
(
'node'
);
if
(
!
$node
->
hasField
(
'field_sponsor'
))
{
return
[];
}
if
(
!
$node
->
field_sponsor
->
isEmpty
())
{
$sponsor_data
=
$this
->
getSponsorData
(
$node
);
}
else
{
$sponsor_data
=
$this
->
initSponsorData
();
if
(
$node
->
hasField
(
'og_group_ref_documentation'
)
&&
!
$node
->
og_group_ref_documentation
->
isEmpty
())
{
// Find the nearest parent with field_sponsor information, and then check the type.
$parent_guide
=
$node
->
og_group_ref_documentation
->
entity
;
$sponsor_info_found
=
FALSE
;
while
(
!
$sponsor_info_found
&&
$parent_guide
)
{
if
(
$parent_guide
->
hasField
(
'field_sponsor'
)
&&
!
$parent_guide
->
field_sponsor
->
isEmpty
())
{
if
(
$parent_guide
->
hasField
(
'field_sponsor_type'
)
&&
$parent_guide
->
field_sponsor_type
->
value
==
"sponsor_all_nested_guides"
)
{
$sponsor_info_found
=
TRUE
;
$sponsor_data
=
$this
->
getSponsorData
(
$parent_guide
);
}
$parent_guide
=
NULL
;
}
else
{
$parent_guide
=
(
$parent_guide
->
hasField
(
'og_group_ref_documentation'
)
&&
!
$parent_guide
->
og_group_ref_documentation
->
isEmpty
())
?
$parent_guide
->
og_group_ref_documentation
->
entity
:
NULL
;
}
}
}
}
return
[
'#theme'
=>
'drupalorg_sponsor_widget'
]
+
$sponsor_data
;
}
/**
* {@inheritdoc}
*/
public
function
getCacheTags
()
{
if
(
$node
=
\Drupal
::
routeMatch
()
->
getParameter
(
'node'
))
{
return
Cache
::
mergeTags
(
parent
::
getCacheTags
(),
[
'node:'
.
$node
->
id
()]);
}
else
{
return
parent
::
getCacheTags
();
}
}
/**
* {@inheritdoc}
*/
public
function
getCacheContexts
()
{
return
Cache
::
mergeContexts
(
parent
::
getCacheContexts
(),
[
'route'
]);
}
/**
* Get the sponsor data given the documentation guide node as the argument.
*
* @param \Drupal\node\NodeInterface $node
* The documentation guide node.
*
* @return array
* An associative array containing the sponsor name, link to the sponsor's site and a link to the logo
*/
protected
function
getSponsorData
(
NodeInterface
$node
)
{
$sponsor_data
=
$this
->
initSponsorData
();
$logo_url
=
""
;
if
(
$node
->
hasField
(
'field_sponsor_alternative_url'
)
&&
!
$node
->
field_sponsor_alternative_url
->
isEmpty
())
{
$organization_url
=
$node
->
get
(
'field_sponsor_alternative_url'
)
->
uri
;
$organization_url
=
Url
::
fromUri
(
$organization_url
)
->
toString
();
}
else
{
$organization_url
=
$node
->
field_sponsor
->
entity
->
get
(
'field_link'
)
->
uri
;
$organization_url
=
Url
::
fromUri
(
$organization_url
)
->
toString
();
}
$organization_name
=
$node
->
field_sponsor
->
entity
->
getTitle
();
$sponsor_data
[
"#organization_name"
]
=
$organization_name
;
$sponsor_data
[
"#organization_url"
]
=
$organization_url
;
if
(
$node
->
field_sponsor
->
entity
->
field_logo
&&
$node
->
field_sponsor
->
entity
->
field_logo
->
entity
)
{
$media_entity
=
$node
->
field_sponsor
->
entity
->
field_logo
->
entity
;
$file_id
=
$media_entity
->
getSource
()
->
getSourceFieldValue
(
$media_entity
);
$logo_file
=
File
::
load
(
$file_id
);
$logo_url
=
ImageStyle
::
load
(
"sponsor_widget_image"
)
->
buildUrl
(
$logo_file
->
getFileUri
());
}
$sponsor_data
[
"#organization_logo"
]
=
$logo_url
;
return
$sponsor_data
;
}
/**
* Initializates the sponsor data array with NULL values.
*
* @return array
* An associative array with NULL values
*/
protected
function
initSponsorData
()
{
$sponsor_data
=
[
"#organization_name"
=>
NULL
,
'#organization_url'
=>
NULL
,
'#organization_logo'
=>
NULL
,
];
return
$sponsor_data
;
}
}
Loading