LinkField.php in Conditional Fields 4.x
File
src/Plugin/conditional_fields/handler/LinkField.php
View source
<?php
namespace Drupal\conditional_fields\Plugin\conditional_fields\handler;
use Drupal\Component\Utility\Tags;
use Drupal\conditional_fields\ConditionalFieldsHandlerBase;
use Drupal\conditional_fields\ConditionalFieldsInterface;
class LinkField extends ConditionalFieldsHandlerBase {
public function statesHandler($field, $field_info, $options) {
$state = [];
switch ($options['values_set']) {
case ConditionalFieldsInterface::CONDITIONAL_FIELDS_DEPENDENCY_VALUES_WIDGET:
$values_array = [];
$uri = $this
->getWidgetValue($options['value_form']);
$values = static::getUriAsDisplayableString($uri);
if (is_array($values)) {
foreach ($values as $value) {
$values_array[] = [
'value' => $value,
];
}
}
else {
$values_array = [
$options['condition'] => $values,
];
}
$state[$options['state']][$options['selector']] = $values_array;
break;
case ConditionalFieldsInterface::CONDITIONAL_FIELDS_DEPENDENCY_VALUES_REGEX:
case ConditionalFieldsInterface::CONDITIONAL_FIELDS_DEPENDENCY_VALUES_XOR:
case ConditionalFieldsInterface::CONDITIONAL_FIELDS_DEPENDENCY_VALUES_OR:
break;
case ConditionalFieldsInterface::CONDITIONAL_FIELDS_DEPENDENCY_VALUES_AND:
break;
}
return $state;
}
private static function getUriAsDisplayableString($uri) {
$scheme = parse_url($uri, PHP_URL_SCHEME);
$displayable_string = $uri;
if ($scheme === 'internal') {
$uri_reference = explode(':', $uri, 2)[1];
$path = parse_url($uri, PHP_URL_PATH);
if ($path === '/') {
$uri_reference = '<front>' . substr($uri_reference, 1);
}
$displayable_string = $uri_reference;
}
elseif ($scheme === 'entity') {
list($entity_type, $entity_id) = explode('/', substr($uri, 7), 2);
$entity_type_manager = \Drupal::entityTypeManager();
if ($entity_type_manager
->getDefinition($entity_type, FALSE) && ($entity = $entity_type_manager
->getStorage($entity_type)
->load($entity_id))) {
$displayable_string = static::getEntityLabels($entity);
}
}
return $displayable_string;
}
private static function getEntityLabels($entity) {
$title = $entity
->label();
$title = Tags::encode($title);
$title_nid = $title . ' (' . $entity
->id() . ')';
$result = [
$title,
$title_nid,
'/node/' . $entity
->id(),
];
return $result;
}
public function getWidgetValue(array $value_form) {
return isset($value_form[0]) && isset($value_form[0]['uri']) ? $value_form[0]['uri'] : '';
}
}
Classes
Name |
Description |
LinkField |
Provides states handler for links provided by the Link module. |