DependencyHelper.php in Conditional Fields 8
File
src/DependencyHelper.php
View source
<?php
namespace Drupal\conditional_fields;
class DependencyHelper {
protected $entity_type;
protected $bundle;
protected $dependent;
protected $dependent_field;
protected $dependencies;
protected $dependent_fields;
protected $uuid;
protected $dependee;
protected $settings;
protected $form_fields;
protected $inheriting_fields;
public function __construct(string $entity_type, string $bundle) {
$this->entity_type = $entity_type;
$this->bundle = $bundle;
$this->module_handler = \Drupal::moduleHandler();
}
public function getAvailableConditionalFields() {
$hook = 'conditional_fields';
$fields = $this->module_handler
->invokeAll($hook, [
$this->entity_type,
$this->bundle,
]);
$this->module_handler
->alter($hook, $fields, $this->entity_type, $this->bundle);
return $fields;
}
public function getBundleDependencies() {
if (!isset($this->dependencies[$this->entity_type][$this->bundle])) {
$this
->resolveBundleDependencies($this
->getBundleDependentFields());
}
return $this->dependencies[$this->entity_type][$this->bundle];
}
public function fieldHasChildren($field) {
return (bool) count($this
->getInheritingFieldNames($field));
}
protected function resolveBundleDependencies($dependent_fields) {
foreach ($dependent_fields as $dependent => $field) {
$this->dependent = $dependent;
$this->dependent_field = $field;
$this
->resolveFieldDependencies();
}
}
protected function resolveFieldDependencies() {
foreach ($this->dependent_field['third_party_settings']['conditional_fields'] as $uuid => $conditional_field) {
$this->uuid = $uuid;
$this->dependee = $conditional_field['dependee'];
$this->settings = $conditional_field['settings'];
if ($this
->fieldDependencyShouldPropagate()) {
if ($this
->fieldDependencyShouldApplyToParent()) {
$this
->registerFieldDependency();
}
$this
->resolveBundleDependencies($this
->getInheritingFields());
continue;
}
$this
->registerFieldDependency();
}
}
protected function fieldDependencyShouldPropagate() {
if (!isset($this->settings['inheritance']['propagate'])) {
return FALSE;
}
return (bool) $this->settings['inheritance']['propagate'];
}
protected function fieldDependencyShouldApplyToParent() {
if (!isset($this->settings['inheritance']['apply_to_parent'])) {
return FALSE;
}
return (bool) $this->settings['inheritance']['apply_to_parent'];
}
protected function fieldDependencyShouldRecurse() {
if (!isset($this->settings['inheritance']['recurse'])) {
return FALSE;
}
return (bool) $this->settings['inheritance']['recurse'];
}
protected function getInheritingFields() {
if (empty($this->dependent_field['third_party_settings']['conditional_fields'][$this->uuid])) {
return [];
}
$propagating_settings = $this->dependent_field['third_party_settings']['conditional_fields'][$this->uuid];
$inheriting_fields = [];
foreach ($this
->getInheritingFieldNames($this->dependent) as $field_name) {
$inheriting_field = $this
->getBundleFormField($field_name);
$new_id = "{$this->uuid}+{$field_name}";
$inheriting_field['third_party_settings']['conditional_fields'][$new_id] = $propagating_settings;
if (!$this
->fieldHasChildren($field_name) || !$this
->fieldDependencyShouldRecurse()) {
unset($inheriting_field['third_party_settings']['conditional_fields'][$new_id]['settings']['inheritance']);
}
$inheriting_fields[$field_name] = $inheriting_field;
}
return $inheriting_fields;
}
protected function getInheritingFieldNames($parent_field) {
if (!isset($this->inheriting_fields)) {
$this->inheriting_fields = $this
->getInheritingChildren();
}
if (!isset($this->inheriting_fields[$parent_field])) {
return [];
}
return $this->inheriting_fields[$parent_field];
}
protected function getInheritingChildren() {
$hook = 'conditional_fields_children';
$inheriting_fields = $this->module_handler
->invokeAll($hook, [
$this->entity_type,
$this->bundle,
]);
$this->module_handler
->alter($hook, $inheriting_fields, $this->entity_type, $this->bundle);
return $inheriting_fields;
}
protected function registerFieldDependency() {
$this
->registerDependent();
$this
->registerDependee();
}
protected function registerDependent() {
$this->dependencies[$this->entity_type][$this->bundle]['dependents'][$this->dependent][$this->uuid] = [
'dependee' => $this->dependee,
'options' => $this->settings,
];
}
protected function registerDependee() {
$this->dependencies[$this->entity_type][$this->bundle]['dependees'][$this->dependee][$this->uuid] = [
'dependent' => $this->dependent,
'options' => $this->settings,
];
}
protected function getBundleDependentFields() {
if (!$this
->bundleHasRegisteredDependentFields()) {
$this
->registerBundleDependentFields();
}
return $this->dependent_fields[$this->entity_type][$this->bundle];
}
protected function bundleHasRegisteredDependentFields() {
if (!isset($this->dependent_fields[$this->entity_type][$this->bundle])) {
return FALSE;
}
if (empty($this->dependent_fields[$this->entity_type][$this->bundle])) {
return FALSE;
}
return TRUE;
}
protected function registerBundleDependentFields() {
$this->dependent_fields[$this->entity_type][$this->bundle] = [];
foreach ($this
->getBundleFormFields() as $name => $field) {
if (!$this
->hasConditionalFields($field)) {
continue;
}
$this->dependent_fields[$this->entity_type][$this->bundle][$name] = $field;
}
}
protected function getBundleFormField($field_name) {
if (!isset($this->form_fields)) {
$this->form_fields = $this
->getBundleFormFields();
}
if (!isset($this->form_fields[$field_name])) {
return [];
}
return $this->form_fields[$field_name];
}
protected function getBundleFormFields() {
$entity = \Drupal::entityTypeManager()
->getStorage('entity_form_display')
->load($this->entity_type . '.' . $this->bundle . '.default');
if (!$entity) {
return [];
}
return $entity
->getComponents();
}
protected function hasConditionalFields($field) {
return !empty($field['third_party_settings']['conditional_fields']);
}
}