Skip to content
Snippets Groups Projects
Commit b3675e86 authored by April Sides's avatar April Sides
Browse files

3063335: Resolved merge conflicts.

parent 346defa4
No related branches found
No related merge requests found
Pipeline #402924 passed with warnings
Showing
with 940 additions and 439 deletions
Addressfield
addressfield
afterbuild
Colan
colan
ergonlogic
Ferreira
Gervais
Hedding
heddn
hideme
itsekhmistro
Magini
mparker
olgarabodzei
oneline
organisation
peterpoe
plusplus
Rabodzei
Rutz
tesformat
Thalles
thalles
Tsekhmistro
UNTRIGGERED
untriggered
autocollapse
autocompleteselect
legendsspan
unrequired
################
# GitLabCI template for Drupal projects.
#
# This template is designed to give any Contrib maintainer everything they need to test, without requiring modification.
# It is also designed to keep up to date with Core Development automatically through the use of include files that can be centrally maintained.
# As long as you include the project, ref and three files below, any future updates added by the Drupal Association will be used in your
# pipelines automatically. However, you can modify this template if you have additional needs for your project.
# The full documentation is on https://project.pages.drupalcode.org/gitlab_templates/
################
# For information on alternative values for 'ref' see https://project.pages.drupalcode.org/gitlab_templates/info/templates-version/
# To test a Drupal 7 project, change the first include filename from .main.yml to .main-d7.yml
include:
- project: $_GITLAB_TEMPLATES_REPO
ref: $_GITLAB_TEMPLATES_REF
file:
- "/includes/include.drupalci.main.yml"
- "/includes/include.drupalci.variables.yml"
- "/includes/include.drupalci.workflows.yml"
################
# Pipeline configuration variables are defined with default values and descriptions in the file
# https://git.drupalcode.org/project/gitlab_templates/-/blob/main/includes/include.drupalci.variables.yml
# Uncomment the lines below if you want to override any of the variables. The following is just an example.
################
variables:
_PHPUNIT_CONCURRENT: '1'
#
# Start custom overrides.
# Based on https://git.drupalcode.org/project/keycdn/-/blob/8.x-1.x/.gitlab-ci.yml
#
variables:
_SHOW_ENVIRONMENT_VARIABLES: 1
# Broaden test coverage.
OPT_IN_TEST_PREVIOUS_MAJOR: 1
# Conditional Fields
Conditional Fields allows you to manage sets of dependencies between fields. When a field is “dependent”, it will only be available for editing and displayed if the state of the “dependee” field matches the right condition.
When editing a node (or any other entity type that supports fields, like users and categories), the dependent fields are dynamically modified with the States API.
A simple use case would be defining a custom “Article teaser" field that is shown only if a "Has teaser" checkbox is checked, but much more complex options are available.
Conditional Fields allows you to manage sets of dependencies between fields.
When a field is “dependent”, it will only be available for editing and displayed
if the state of the “dependee” field matches the right condition.
When editing a node (or any other entity type that supports fields, like users
and categories), the dependent fields are dynamically modified with the States
API. A simple use case would be defining a custom “Article teaser" field that is
shown only if a "Has teaser" checkbox is checked, but much more complex options
are available.
For a full description of the module, visit the
[project page](https://www.drupal.org/project/conditional_fields).
......@@ -33,7 +38,8 @@ information, see
## Configuration
After enable the module, you can create conditional fields on /admin/structure/conditional_fields.
After enable the module, you can create conditional fields on
/admin/structure/conditional_fields.
## Maintainers
......
{
"name": "drupal/conditional_fields",
"description": "Enables conditional field behaviors.",
"type":"drupal-module",
"license": "GPL-2.0-or-later",
"minimum-stability": "dev",
"homepage": "https://www.drupal.org/project/conditional_fields",
"support": {
"issues": "https://www.drupal.org/project/issues/conditional_fields",
"source": "https://git.drupalcode.org/project/conditional_fields"
},
"require-dev": {
"drupal/entity_reference_revisions": "1.x-dev",
"drupal/paragraphs": "^1"
}
}
name: Conditional Fields
type: module
description: Define dependencies between fields based on their states and values.
core: 8.x
core_version_requirement: ^8 || ^9 || ^10
core_version_requirement: ^9 || ^10 || ^11
configure: conditional_fields
package: Fields
dependencies:
......
......@@ -16,13 +16,13 @@ function conditional_fields_update_8001() {
$bundle_info = \Drupal::service('entity_type.bundle.info');
$entity_types = $cf_service->getEntityTypes();
foreach ($entity_types as $entity_type) {
$etid = $entity_type->id();
$bundles = $bundle_info->getBundleInfo($etid);
$entity_id = $entity_type->id();
$bundles = $bundle_info->getBundleInfo($entity_id);
foreach ($bundles as $bundle_name => $bundle) {
/** @var \Drupal\Core\Entity\Entity\EntityFormDisplay $entity */
$entity = \Drupal::entityTypeManager()
->getStorage('entity_form_display')
->load("$etid.$bundle_name.default");
->load("$entity_id.$bundle_name.default");
// Replace keys.
if ($entity instanceof EntityInterface) {
_conditional_fields_update_conditional_fields_settings($entity);
......
conditional_fields:
version: "4.x"
js:
js/conditional_fields.js: {}
dependencies:
......@@ -7,7 +6,6 @@ conditional_fields:
- core/drupal.states
admin:
version: "4.x"
css:
component:
css/conditional_fields.admin.css: {}
......@@ -5,5 +5,3 @@ config.conditional_field:
description: 'List Conditional fields'
parent: system.admin_structure
weight: 100
conditional_fields.conditional_fields.node:
title: Manage Dependencies
title: Manage dependencies
route_name: conditional_fields.tab.node
base_route: entity.node_type.edit_form
weight: 10
conditional_fields.conditional_fields.media:
title: Manage Dependencies
title: Manage dependencies
route_name: conditional_fields.tab.media
base_route: entity.media_type.edit_form
weight: 10
conditional_fields.conditional_fields.block:
title: Manage Dependencies
title: Manage dependencies
route_name: conditional_fields.tab.block_content
base_route: entity.block_content_type.edit_form
weight: 10
conditional_fields.conditional_fields.comment:
title: Manage Dependencies
title: Manage dependencies
route_name: conditional_fields.tab.comment
base_route: entity.comment_type.edit_form
weight: 10
conditional_fields.conditional_fields.user:
title: Manage Dependencies
title: Manage dependencies
route_name: conditional_fields.tab.user
base_route: entity.user.admin_form
weight: 10
conditional_fields.conditional_fields.paragraphs_type:
title: Manage dependencies
route_name: conditional_fields.tab.paragraph
base_route: entity.paragraphs_type.edit_form
weight: 10
......@@ -5,11 +5,11 @@
* Contains conditional_fields.module.
*/
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\WidgetInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Url;
/**
......@@ -51,10 +51,11 @@ function conditional_fields_theme() {
*/
function conditional_fields_entity_operation(EntityInterface $entity) {
$operations = [];
if ($entity->getEntityTypeId() == 'node_type') {
$url = Url::fromRoute('conditional_fields.tab.node', [$entity->getEntityTypeId() => $entity->id()]);
if ($entity->getEntityTypeId() == 'node_type' && $url->access()) {
$operations['dependencies'] = [
'title' => t('Manage dependencies'),
'url' => Url::fromRoute('conditional_fields.tab.node', [$entity->getEntityTypeId() => $entity->id()]),
'url' => $url,
'weight' => 29,
];
}
......@@ -95,7 +96,7 @@ function conditional_fields_conditional_fields_alter(&$fields, $entity_type, $bu
}
/**
* After build function to process field depencies.
* After build function to process field dependencies.
*/
function conditional_fields_element_after_build($element, FormStateInterface &$form_state) {
return \Drupal::service('conditional_fields.element_alter_helper')->afterBuild($element, $form_state);
......@@ -109,7 +110,7 @@ function conditional_fields_element_after_build($element, FormStateInterface &$f
* @param array $field
* The field form element.
*
* @return string|FALSE
* @return string|false
* A jQuery selector string.
*/
function conditional_fields_field_selector($field) {
......@@ -158,13 +159,6 @@ function conditional_fields_field_widget_third_party_settings_form(WidgetInterfa
* Implements hook_form_alter().
*
* Hook_form_alter() that attaches an 'afterbuild' function to the form.
*
* @param $form
* The form to attach the callback to.
* @param $form_state
* Not used, part of the hook interface.
* @param $form_id
* Not used, part of the hook interface.
*/
function conditional_fields_form_alter(&$form, &$form_state, $form_id) {
$form['#after_build'][] = 'conditional_fields_form_after_build';
......@@ -173,30 +167,44 @@ function conditional_fields_form_alter(&$form, &$form_state, $form_id) {
/**
* Implements hook_inline_entity_form_entity_form_alter().
*
* Hook_inline_entity_form_entity_form_alter() that attaches an 'afterbuild' function to the form.
*
* @param array $entity_form
* Nested array of form elements that comprise the entity form.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The form state of the parent form.
* Hook_inline_entity_form_entity_form_alter() that attaches an 'afterbuild'
* function to the form.
*/
function conditional_fields_inline_entity_form_entity_form_alter(&$entity_form, &$form_state) {
$entity_form['#after_build'][] = 'conditional_fields_form_after_build';
}
/**
* Runs the 'afterbuild' validation of a form with dependencies.
* Implements hook_form_after_build().
*
* Runs the 'afterbuild' validation of a form with dependencies.
* This function will run only once per form.
*
* @param $form
* The form to validate
* @param $form_state
* The form's state.
*
* @return mixed
* The form.
*/
function conditional_fields_form_after_build($form, &$form_state) {
return \Drupal::service('conditional_fields.form_helper')->afterBuild($form, $form_state);
}
/**
* Allows identify dependents/dependees with predictable IDs.
*/
function conditional_fields_get_simpler_id($id) {
// Removes an appended identifier used for individual options.
// preg_match('/.*([-]{2,}[a-zA-Z0-9]+)$/', $id, $matches);.
preg_match('/.*(--[a-zA-Z0-9-_]+)$/', $id, $matches);
if ($matches[1] ?? FALSE) {
$id = str_replace($matches[1], '', $id);
}
$strings_to_remove = [
'-0-value',
'-value',
];
foreach ($strings_to_remove as $string_to_remove) {
$string_to_remove_strlen = strlen($string_to_remove);
if ((strrpos($id, $string_to_remove) + $string_to_remove_strlen) == strlen($id)) {
return substr($id, 0, -$string_to_remove_strlen);
}
}
return $id;
}
......@@ -6,4 +6,3 @@ edit conditional fields:
delete conditional fields:
title: 'Delete Conditional fields'
......@@ -40,7 +40,7 @@ conditional_fields.delete_form:
conditional_fields.tab.node:
path: '/admin/structure/types/manage/{node_type}/conditionals'
defaults:
_title: 'Manage Dependencies'
_title: 'Manage dependencies'
_controller: 'conditional_fields.controller:provideArguments'
requirements:
_permission: 'view conditional fields'
......@@ -48,7 +48,7 @@ conditional_fields.tab.node:
conditional_fields.tab.media:
path: '/admin/structure/media/manage/{media_type}/conditionals'
defaults:
_title: 'Manage Dependencies'
_title: 'Manage dependencies'
_controller: 'conditional_fields.controller:getMediaEditFormTab'
requirements:
_permission: 'view conditional fields'
......@@ -56,7 +56,7 @@ conditional_fields.tab.media:
conditional_fields.tab.block_content:
path: '/admin/structure/block/manage/block-content/{block_content_type}/conditionals'
defaults:
_title: 'Manage Dependencies'
_title: 'Manage dependencies'
_controller: 'conditional_fields.controller:getBlockEditFormTab'
requirements:
_permission: 'view conditional fields'
......@@ -64,7 +64,7 @@ conditional_fields.tab.block_content:
conditional_fields.tab.comment:
path: '/admin/structure/comment/manage/{comment_type}/conditionals'
defaults:
_title: 'Manage Dependencies'
_title: 'Manage dependencies'
_controller: 'conditional_fields.controller:getCommentEditFormTab'
requirements:
_permission: 'view conditional fields'
......@@ -72,11 +72,19 @@ conditional_fields.tab.comment:
conditional_fields.tab.user:
path: '/admin/config/people/accounts/conditionals'
defaults:
_title: 'Manage Dependencies'
_title: 'Manage dependencies'
_controller: 'conditional_fields.controller:getUserEditFormTab'
requirements:
_permission: 'view conditional fields'
conditional_fields.tab.paragraph:
path: '/admin/structure/paragraphs_type/{paragraphs_type}/conditionals'
defaults:
_title: 'Manage dependencies'
_controller: 'conditional_fields.controller:getParagraphEditFormTab'
requirements:
_permission: 'view conditional fields'
conditional_fields.edit_form.tab:
path: '/admin/structure/conditional_fields/{entity_type}/{bundle}/{field_name}/{uuid}/tab/edit'
defaults:
......
......@@ -7,10 +7,20 @@ services:
parent: default_plugin_manager
conditional_fields.controller:
class: Drupal\conditional_fields\Controller\ConditionalFieldController
arguments: ['@entity_type.manager', '@form_builder', '@entity_type.bundle.info', '@entity_field.manager']
arguments:
- '@entity_type.manager'
- '@form_builder'
- '@entity_type.bundle.info'
- '@entity_field.manager'
conditional_fields.form_helper:
class: Drupal\conditional_fields\ConditionalFieldsFormHelper
arguments: ['@plugin.manager.element_info', '@plugin.manager.conditional_fields_handlers', '@entity_display.repository']
arguments:
- '@plugin.manager.element_info'
- '@plugin.manager.conditional_fields_handlers'
- '@entity_display.repository'
- '@language_manager'
conditional_fields.element_alter_helper:
class: Drupal\conditional_fields\ConditionalFieldsElementAlterHelper
arguments: []
arguments:
- '@module_handler'
- '@entity_type.manager'
.conditional-fields-selector {
min-width: 150px;
width: 50%;
min-width: 150px;
}
This diff is collapsed.
logo.png 0 → 100644
logo.png

8.67 KiB

parameters:
ignoreErrors:
-
message: "#^Variable \\$selectors in empty\\(\\) is never defined\\.$#"
count: 1
path: src/Plugin/conditional_fields/handler/TextAreaFormatted.php
-
message: "#^Undefined variable\\: \\$selectors$#"
count: 1
path: src/Plugin/conditional_fields/handler/TextAreaFormatted.php
-
message: "#^Variable \\$regex in empty\\(\\) is never defined\\.$#"
count: 1
path: src/Plugin/conditional_fields/handler/LanguageSelect.php
includes:
- phpstan-baseline.neon
parameters:
level: 1
......@@ -4,6 +4,8 @@ namespace Drupal\conditional_fields;
use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Form\FormStateInterface;
/**
......@@ -11,10 +13,31 @@ use Drupal\Core\Form\FormStateInterface;
*/
class ConditionalFieldsElementAlterHelper {
/**
* The module handler.
*
* @var \Drupal\Core\Extension\ModuleHandlerInterface
*/
protected $moduleHandler;
/**
* Provides an interface for entity type managers.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* ConditionalFieldsElementAlterHelper constructor.
*
* @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
* The module handler.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager.
*/
public function __construct() {
public function __construct(ModuleHandlerInterface $module_handler, EntityTypeManagerInterface $entity_type_manager) {
$this->moduleHandler = $module_handler;
$this->entityTypeManager = $entity_type_manager;
}
/**
......@@ -43,6 +66,8 @@ class ConditionalFieldsElementAlterHelper {
return $element;
}
$is_related_to_paragraph = FALSE;
$full_form = &$form_state->getCompleteForm();
// Some fields do not have entity type and bundle properties.
// In this case we try to use the properties from the form.
......@@ -69,13 +94,16 @@ class ConditionalFieldsElementAlterHelper {
$bundle = $entity->bundle();
$entity_type = $entity->getEntityTypeId();
/**
* @deprecated Not actual from Drupal 8.7.0.
* Media entity returns the actual bundle object, rather than id
*/
// Deprecated, not actual from Drupal 8.7.0.
// Media entity returns the actual bundle object, rather than id.
if (is_object($bundle) && method_exists($bundle, 'getPluginId')) {
$bundle = $bundle->getPluginId();
}
$paragraph_bundle = $this->getParagraphBundle($field, $form);
$bundle = $paragraph_bundle ?: $bundle;
$is_related_to_paragraph = (bool) $paragraph_bundle;
$entity_type = $is_related_to_paragraph ? 'paragraph' : $entity_type;
}
}
}
......@@ -123,19 +151,57 @@ class ConditionalFieldsElementAlterHelper {
return $element;
}
$field_name = reset($field['#array_parents']);
// We get the name of the field inside the the paragraph where the
// conditions are being applied, instead of the field name where the
// paragraph is.
if ($is_related_to_paragraph) {
foreach ($field['#array_parents'] as $parent) {
if (isset($dependencies['dependents'][$parent])) {
$field_name = $parent;
break;
}
if (isset($dependencies['dependees'][$parent])) {
$field_name = $parent;
break;
}
}
if ($parent != $field_name || $first_parent == $field_name || !isset($field['#type'])) {
return $element;
}
}
$paragraph_info = [];
if ($is_related_to_paragraph) {
$paragraph_info['entity_type'] = $entity_type;
$paragraph_info['bundle'] = $bundle;
$paragraph_info['paragraph_field'] = $first_parent;
$paragraph_info['array_parents'] = $element['#array_parents'];
}
// Attach dependent.
if (isset($dependencies['dependents'][$field_name])) {
foreach ($dependencies['dependents'][$field_name] as $id => $dependency) {
if (!isset($form['#conditional_fields'][$field_name]['dependees'][$id]) || $this->isPriorityField($field)) {
$this->attachDependency($form, $form_state, ['#field_name' => $dependency['dependee']], $field, $dependency['options'], $id);
if ($is_related_to_paragraph) {
$paragraph_info['field'] = $field_name;
}
$this->attachDependency($form, $form_state, ['#field_name' => $dependency['dependee']], $field, $dependency['options'], $id, $paragraph_info);
}
}
}
// Attach dependee.
if (isset($dependencies['dependees'][$field_name])) {
foreach ($dependencies['dependees'][$field_name] as $id => $dependency) {
if (!isset($form['#conditional_fields'][$field_name]['dependents'][$id]) || $this->isPriorityField($field)) {
$this->attachDependency($form, $form_state, $field, ['#field_name' => $dependency['dependent']], $dependency['options'], $id);
if ($is_related_to_paragraph) {
$paragraph_info['field'] = $field_name;
}
$this->attachDependency($form, $form_state, $field, ['#field_name' => $dependency['dependent']], $dependency['options'], $id, $paragraph_info);
}
}
}
......@@ -143,6 +209,32 @@ class ConditionalFieldsElementAlterHelper {
return $element;
}
/**
* Gets the paragraph's bundle from a form field.
*/
public function getParagraphBundle($field, $form) {
$closest_parent = [];
$last_subform = FALSE;
// Finds closest subform position.
foreach ($field['#array_parents'] as $index => $parent) {
if ($parent === 'subform') {
$last_subform = $index;
}
}
if (!$last_subform) {
return FALSE;
}
// Gets the route to the closest subform.
$closest_parent = array_slice($field['#array_parents'], 0, $last_subform);
// Gets the paragraph's bundle if any...
$bundle = NestedArray::getValue($form, array_merge($closest_parent, ['#paragraph_type']));
return $bundle && !is_array($bundle) ? $bundle : FALSE;
}
/**
* Loads all dependencies from the database for a given bundle.
*/
......@@ -150,7 +242,8 @@ class ConditionalFieldsElementAlterHelper {
static $dependency_helpers;
$dependency_key = $entity_type . '.' . $bundle;
if (!isset($dependency_helpers[$dependency_key])) {
$dependency_helpers[$dependency_key] = new DependencyHelper($entity_type, $bundle);
$dependency_helpers[$dependency_key] =
new DependencyHelper($entity_type, $bundle, $this->moduleHandler, $this->entityTypeManager);
}
return $dependency_helpers[$dependency_key]->getBundleDependencies();
}
......@@ -158,7 +251,8 @@ class ConditionalFieldsElementAlterHelper {
/**
* Checking if field is priority for rewrite the conditions.
*
* If the field widget is datelist this function help to return correct object for this field.
* If the field widget is datelist this function help to return correct object
* for this field.
*
* @param array $field
* The field form element.
......@@ -171,7 +265,7 @@ class ConditionalFieldsElementAlterHelper {
'datelist',
];
// For modules supports.
\Drupal::moduleHandler()->alter(['conditional_fields_priority_field'], $priority_fields);
$this->moduleHandler->alter(['conditional_fields_priority_field'], $priority_fields);
if (isset($field['#type']) && in_array($field['#type'], $priority_fields)) {
return TRUE;
......@@ -187,14 +281,16 @@ class ConditionalFieldsElementAlterHelper {
*
* @param array $form
* The form where the dependency is attached.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current state of the form.
* @param string $dependee
* The dependee field form element. Either a string identifying the element
* key in the form, or a fully built field array. Actually used properties of
* the array are #field_name and #parents.
* key in the form, or a fully built field array. Actually used properties
* of the array are #field_name and #parents.
* @param string $dependent
* The dependent field form element. Either a string identifying the element
* key in the form, or a fully built field array. Actually used properties of
* the array are #field_name and #field_parents.
* key in the form, or a fully built field array. Actually used properties
* of the array are #field_name and #field_parents.
* @param array $options
* An array of dependency options with the following key/value pairs:
* - state: The state applied to the dependent when the dependency is
......@@ -202,42 +298,49 @@ class ConditionalFieldsElementAlterHelper {
* - condition: The condition for the dependency to be triggered. See
* conditionalFieldsConditions() for available conditions.
* - values_set: One of the following constants:
* - ConditionalFieldsInterface::CONDITIONAL_FIELDS_DEPENDENCY_VALUES_WIDGET: Dependency is
* triggered if the dependee has a certain value defined in 'value'.
* - ConditionalFieldsInterface::CONDITIONAL_FIELDS_DEPENDENCY_VALUES_AND: Dependency is triggered if
* the dependee has all the values defined in 'values'.
* - ConditionalFieldsInterface::CONDITIONAL_FIELDS_DEPENDENCY_VALUES_OR: Dependency is triggered if the
* dependee has any of the values defined in 'values'.
* - ConditionalFieldsInterface::CONDITIONAL_FIELDS_DEPENDENCY_VALUES_XOR: Dependency is triggered if
* the dependee has only one of the values defined in 'values'.
* - ConditionalFieldsInterface::CONDITIONAL_FIELDS_DEPENDENCY_VALUES_NOT: Dependency is triggered if
* the dependee does not have any of the values defined in 'values'.
* - ConditionalFieldsInterface::CONDITIONAL_FIELDS_DEPENDENCY_VALUES_WIDGET:
* Dependency is triggered if the dependee has a certain value defined
* in 'value'.
* - ConditionalFieldsInterface::CONDITIONAL_FIELDS_DEPENDENCY_VALUES_AND:
* Dependency is triggered if the dependee has all the values defined in
* 'values'.
* - ConditionalFieldsInterface::CONDITIONAL_FIELDS_DEPENDENCY_VALUES_OR:
* Dependency is triggered if the dependee has any of the values defined
* in 'values'.
* - ConditionalFieldsInterface::CONDITIONAL_FIELDS_DEPENDENCY_VALUES_XOR:
* Dependency is triggered if the dependee has only one of the values
* defined in 'values'.
* - ConditionalFieldsInterface::CONDITIONAL_FIELDS_DEPENDENCY_VALUES_NOT:
* Dependency is triggered if the dependee does not have any of the
* values defined in 'values'.
* - value: The value to be tested when 'values_set' is
* ConditionalFieldsInterface::CONDITIONAL_FIELDS_DEPENDENCY_VALUES_WIDGET. An associative array with
* the same structure of the dependee field values as found in
* $form_states['values] when the form is submitted. You can use
* field_default_extract_form_values() to extract this array.
* ConditionalFieldsInterface::CONDITIONAL_FIELDS_DEPENDENCY_VALUES_WIDGET.
* An associative array with the same structure of the dependee field
* values as found in $form_states['values] when the form is submitted.
* You can use field_default_extract_form_values() to extract this array.
* - values: The array of values to be tested when 'values_set' is not
* ConditionalFieldsInterface::CONDITIONAL_FIELDS_DEPENDENCY_VALUES_WIDGET.
* - value_form: An associative array with the same structure of the dependee
* field values as found in $form_state['input']['value']['field'] when the
* form is submitted.
* - value_form: An associative array with the same structure of the
* dependee field values as found in
* $form_state['input']['value']['field'] when the form is submitted.
* - effect: The jQuery effect associated to the state change. See
* conditionalFieldsEffects() for available effects and options.
* - effect_options: The options for the active effect.
* - selector: (optional) Custom jQuery selector for the dependee.
* @param int $id
* (internal use) The identifier for the dependency. Omit this parameter when
* attaching a custom dependency.
* (internal use) The identifier for the dependency. Omit this parameter
* when attaching a custom dependency.
* @param array $paragraph_info
* @todo Document parameter.
*
* Note that you don't need to manually set all these options, since default
* settings are always provided.
*/
public function attachDependency(array &$form, &$form_state, $dependee, $dependent, array $options, $id = 0) {
public function attachDependency(array &$form, &$form_state, $dependee, $dependent, array $options, $id = 0, $paragraph_info = []) {
// The absence of the $id parameter identifies a custom dependency.
if (!$id) {
// String values are accepted to simplify usage of this function with custom
// forms.
// String values are accepted to simplify usage of this function with
// custom forms.
if (is_string($dependee) && is_string($dependent)) {
$dependee = [
'#field_name' => $dependee,
......@@ -261,9 +364,19 @@ class ConditionalFieldsElementAlterHelper {
// Attach dependee.
// Use the #array_parents property of the dependee instead of #field_parents
// since we will need access to the full structure of the widget.
if (isset($dependee['#parents'])) {
$form['#conditional_fields'][$dependee['#parents'][0]]['parents'] = $dependee['#array_parents'];
$form['#conditional_fields'][$dependee['#parents'][0]]['dependents'][$id] = [
if (isset($dependee['#array_parents'])) {
$dependee_index = $dependee['#parents'][0];
if ($paragraph_info) {
$dependee_index = conditional_fields_get_simpler_id($dependee['#id']);
}
$form['#conditional_fields'][$dependee_index]['index'] = $dependee_index;
$form['#conditional_fields'][$dependee_index]['base'] = $dependee_index;
$form['#conditional_fields'][$dependee_index]['is_from_paragraph'] = (bool) $paragraph_info;
$form['#conditional_fields'][$dependee_index]['parents'] = $dependee['#array_parents'];
$form['#conditional_fields'][$dependee_index]['dependents'][$id] = [
'dependent' => $dependent['#field_name'],
'options' => $options,
];
......@@ -272,8 +385,8 @@ class ConditionalFieldsElementAlterHelper {
// Attach dependent.
if (!empty($dependent['#parents'])) {
$dependent_parents = $dependent['#parents'];
// If the field type is Date, we need to remove the last "date" parent key,
// since it is not part of the $form_state value when we validate it.
// If the field type is Date, we need to remove the last "date" parent
// key, since it is not part of the $form_state value when we validate it.
if (isset($dependent['#type']) && $dependent['#type'] === 'date') {
array_pop($dependent_parents);
}
......@@ -281,9 +394,21 @@ class ConditionalFieldsElementAlterHelper {
elseif (isset($dependent['#field_parents'])) {
$dependent_parents = $dependent['#field_parents'];
}
if (isset($dependent_parents)) {
$form['#conditional_fields'][$dependent['#parents'][0]]['field_parents'] = $dependent_parents;
$form['#conditional_fields'][$dependent['#parents'][0]]['dependees'][$id] = [
$dependent_index = $dependent['#parents'][0];
if ($paragraph_info) {
$dependent_index = conditional_fields_get_simpler_id($dependent['#id']);
}
$form['#conditional_fields'][$dependent_index]['is_from_paragraph'] = (bool) $paragraph_info;
$form['#conditional_fields'][$dependent_index]['index'] = $dependent_index;
$form['#conditional_fields'][$dependent_index]['field_parents'] = $dependent_parents;
$form['#conditional_fields'][$dependent_index]['array_parents'] = $paragraph_info['array_parents'] ?? [];
$form['#conditional_fields'][$dependent_index]['dependees'][$id] = [
'dependee' => $dependee['#field_name'],
'options' => $options,
];
......
This diff is collapsed.
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment