This repository has been archived by the owner on Dec 8, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #23 from IUCN-ELC/develop
New field: source_website_url
- Loading branch information
Showing
11 changed files
with
281 additions
and
49 deletions.
There are no files selected for viewing
97 changes: 53 additions & 44 deletions
97
config/default/core.entity_form_display.node.court_decision.default.yml
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
config/default/field.field.node.court_decision.field_source_website.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
uuid: 7c7f4915-6f5a-41e2-aba1-cdb607d355d9 | ||
langcode: en | ||
status: true | ||
dependencies: | ||
config: | ||
- field.storage.node.field_source_website | ||
- node.type.court_decision | ||
module: | ||
- link | ||
id: node.court_decision.field_source_website | ||
field_name: field_source_website | ||
entity_type: node | ||
bundle: court_decision | ||
label: 'Source Website' | ||
description: '' | ||
required: false | ||
translatable: false | ||
default_value: { } | ||
default_value_callback: '' | ||
settings: | ||
link_type: 17 | ||
title: 0 | ||
field_type: link |
19 changes: 19 additions & 0 deletions
19
config/default/field.storage.node.field_source_website.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
uuid: 187cf192-0b94-4cc0-ae98-a83caeb96808 | ||
langcode: en | ||
status: true | ||
dependencies: | ||
module: | ||
- link | ||
- node | ||
id: node.field_source_website | ||
field_name: field_source_website | ||
entity_type: node | ||
type: link | ||
settings: { } | ||
module: link | ||
locked: false | ||
cardinality: 1 | ||
translatable: true | ||
indexes: { } | ||
persist_with_no_fields: false | ||
custom_storage: false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
170 changes: 170 additions & 0 deletions
170
...modules/custom/wildlex_structure/src/Plugin/Field/FieldFormatter/labelAsLinkFormatter.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,170 @@ | ||
<?php | ||
|
||
namespace Drupal\wildlex_structure\Plugin\Field\FieldFormatter; | ||
|
||
use Drupal\Core\Field\FieldItemListInterface; | ||
use Drupal\Core\Field\FormatterBase; | ||
use Drupal\Core\Form\FormStateInterface; | ||
use Drupal\Core\Entity\EntityInterface; | ||
use Drupal\Core\Entity\Exception\UndefinedLinkTemplateException; | ||
use Drupal\Core\Field\Plugin\Field\FieldFormatter\EntityReferenceFormatterBase; | ||
use Drupal\Core\Url; | ||
|
||
use Drupal\Component\Utility\Unicode; | ||
use Drupal\Core\Field\FieldDefinitionInterface; | ||
|
||
|
||
|
||
use Drupal\Core\Path\PathValidatorInterface; | ||
use Drupal\Core\Plugin\ContainerFactoryPluginInterface; | ||
|
||
use Drupal\link\LinkItemInterface; | ||
use Symfony\Component\DependencyInjection\ContainerInterface; | ||
|
||
|
||
|
||
/** | ||
* Plugin implementation of the 'label_as_link_formatter' formatter. | ||
* | ||
* @FieldFormatter( | ||
* id = "label_as_link_formatter", | ||
* label = @Translation("Label as link"), | ||
* field_types = { | ||
* "entity_reference" | ||
* } | ||
* ) | ||
*/ | ||
class labelAsLinkFormatter extends EntityReferenceFormatterBase { | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public static function defaultSettings() { | ||
return [ | ||
'link_field' => '', | ||
'target' => '', | ||
] + parent::defaultSettings(); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function settingsForm(array $form, FormStateInterface $form_state) { | ||
$elements['link_field'] = [ | ||
'#title' => t('Get the link from another link field.'), | ||
'#type' => 'textfield', | ||
'#description' => 'machine name associated with the link type field', | ||
'#default_value' => $this->getSetting('link_field'), | ||
]; | ||
$elements['target'] = [ | ||
'#type' => 'checkbox', | ||
'#title' => t('Open link in new window'), | ||
'#return_value' => '_blank', | ||
'#default_value' => $this->getSetting('target'), | ||
]; | ||
|
||
return $elements; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function settingsSummary() { | ||
$summary = []; | ||
$summary[] = $this->getSetting('link_field') ? t('Link from <em>@field</em>', ['@field' => $this->getSetting('link_field')]) : t('No link'); | ||
return $summary; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function viewElements(FieldItemListInterface $items, $langcode) { | ||
$elements = []; | ||
$uri = FALSE; | ||
$link_field = $this->getSetting('link_field'); | ||
|
||
/* @var \Drupal\node\Entity\Node $node */ | ||
$node = $items->getEntity(); | ||
if ($node && $node->{$link_field}) { | ||
if ($node->{$link_field}->getFieldDefinition()->getType() == 'link') { | ||
foreach ($node->{$link_field} as $delta => $item) { | ||
// By default use the full URL as the link text. | ||
$uri = $this->buildUrl($item); | ||
} | ||
} | ||
} | ||
if ($this->getEntitiesToView($items, $langcode)) { | ||
foreach ($this->getEntitiesToView($items, $langcode) as $delta => $entity) { | ||
$label = $entity->label(); | ||
if ($uri && !$entity->isNew()) { | ||
$elements[$delta] = [ | ||
'#type' => 'link', | ||
'#title' => $label, | ||
'#url' => $uri, | ||
'#options' => $uri->getOptions(), | ||
]; | ||
|
||
if (!empty($items[$delta]->_attributes)) { | ||
$elements[$delta]['#options'] += ['attributes' => []]; | ||
$elements[$delta]['#options']['attributes'] += $items[$delta]->_attributes; | ||
// Unset field item attributes since they have been included in the | ||
// formatter output and shouldn't be rendered in the field template. | ||
unset($items[$delta]->_attributes); | ||
} | ||
} | ||
else { | ||
$elements[$delta] = ['#plain_text' => $label]; | ||
} | ||
$elements[$delta]['#cache']['tags'] = $entity->getCacheTags(); | ||
} | ||
} else { | ||
if ($uri) { | ||
$elements[] = [ | ||
'#type' => 'link', | ||
'#title' => $uri->toString(), | ||
'#url' => $uri, | ||
'#options' => $uri->getOptions(), | ||
]; | ||
|
||
if (!empty($items[$delta]->_attributes)) { | ||
$elements[$delta]['#options'] += ['attributes' => []]; | ||
$elements[$delta]['#options']['attributes'] += $items[$delta]->_attributes; | ||
// Unset field item attributes since they have been included in the | ||
// formatter output and shouldn't be rendered in the field template. | ||
unset($items[$delta]->_attributes); | ||
} | ||
} | ||
} | ||
|
||
|
||
|
||
return $elements; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function checkAccess(EntityInterface $entity) { | ||
return $entity->access('view label', NULL, TRUE); | ||
} | ||
|
||
protected function buildUrl(LinkItemInterface $item) { | ||
$url = $item->getUrl() ?: Url::fromRoute('<none>'); | ||
|
||
$settings = $this->getSettings(); | ||
$options = $item->options; | ||
$options += $url->getOptions(); | ||
|
||
// Add optional 'rel' attribute to link options. | ||
if (!empty($settings['rel'])) { | ||
$options['attributes']['rel'] = $settings['rel']; | ||
} | ||
// Add optional 'target' attribute to link options. | ||
if (!empty($settings['target'])) { | ||
$options['attributes']['target'] = $settings['target']; | ||
} | ||
$url->setOptions($options); | ||
|
||
return $url; | ||
} | ||
|
||
} |
5 changes: 5 additions & 0 deletions
5
web/modules/custom/wildlex_structure/wildlex_structure.info.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
name: 'wildlex_structure' | ||
type: module | ||
description: 'Wildlex misc functionalities.' | ||
core: 8.x | ||
package: 'Custom' |