You are here

class WidgetDefaults in Simple hierarchical select 8

Same name and namespace in other branches
  1. 2.0.x src/WidgetDefaults.php \Drupal\shs\WidgetDefaults

Helper class for setting default values on SHS widgets.

Hierarchy

Expanded class hierarchy of WidgetDefaults

1 file declares its use of WidgetDefaults
OptionsShsWidget.php in src/Plugin/Field/FieldWidget/OptionsShsWidget.php
1 string reference to 'WidgetDefaults'
shs.services.yml in ./shs.services.yml
shs.services.yml
1 service uses WidgetDefaults
shs.widget_defaults in ./shs.services.yml
Drupal\shs\WidgetDefaults

File

src/WidgetDefaults.php, line 10

Namespace

Drupal\shs
View source
class WidgetDefaults implements WidgetDefaultsInterface {

  /**
   * The entity type manager service.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * WidgetDefaults constructor.
   *
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager service.
   */
  public function __construct(EntityTypeManagerInterface $entity_type_manager) {
    $this->entityTypeManager = $entity_type_manager;
  }

  /**
   * Gets an initial default values array.
   *
   * @param string $default_value
   *   The default value.
   * @param int $cardinality
   *   (optional) The field's cardinality. Defaults to 1.
   *
   * @return array
   *   The initial default value array.
   */
  public function getInitialParentDefaults($default_value, $cardinality = 1) {
    $parents = [];

    // In case of unlimited(-1) we also need one iteration.
    if ($cardinality < 0) {
      $cardinality = 1;
    }
    for ($i = 1; $i <= $cardinality; $i++) {
      $parents[] = [
        [
          'parent' => 0,
          'defaultValue' => $default_value,
        ],
      ];
    }
    return $parents;
  }

  /**
   * Load parents for default values.
   *
   * @param array|string $default_values
   *   List of default values of the widget.
   * @param string $any_value
   *   The value to assign to "any value".
   * @param string $entity_type
   *   The entity type the select is displaying.
   * @param int $cardinality
   *   The field's cardinality.
   *
   * @return array
   *   List of parents for each default value.
   */
  public function getParentDefaults($default_values, $any_value, $entity_type, $cardinality = 1) {
    $parents = [];
    if (!is_array($default_values)) {
      $default_values = [
        $default_values,
      ];
    }
    foreach ($default_values as $delta => $value) {
      if ($any_value === $value) {
        $parents[$delta] = [
          [
            'parent' => 0,
            'defaultValue' => $any_value,
          ],
        ];
        continue;
      }
      try {
        $parent_terms = array_reverse(array_keys(shs_term_load_all_parents($value)));
        $keys = array_merge([
          0,
        ], $parent_terms);
        $values = array_merge($parent_terms, [
          $value,
        ]);
        $parents[$delta] = [];
        foreach ($keys as $index => $key) {
          $parents[$delta][] = [
            'parent' => $key,
            'defaultValue' => $values[$index] ?: $any_value,
          ];
        }
      } catch (\Exception $ex) {
        $parents[$delta] = [
          [
            'parent' => 0,
            'defaultValue' => $any_value,
          ],
        ];
      }
    }

    // Populate the parents array so it's equal to the field's cardinality.
    if ($cardinality < 0) {
      $cardinality = 1;
    }
    $parents_count = count($parents);
    if ($parents_count >= $cardinality) {
      return $parents;
    }
    for ($i = 1; $i <= $cardinality - $parents_count; $i++) {
      $parents[] = [
        [
          'parent' => 0,
          'defaultValue' => $any_value,
        ],
      ];
    }
    return $parents;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
WidgetDefaults::$entityTypeManager protected property The entity type manager service.
WidgetDefaults::getInitialParentDefaults public function Gets an initial default values array. Overrides WidgetDefaultsInterface::getInitialParentDefaults
WidgetDefaults::getParentDefaults public function Load parents for default values. Overrides WidgetDefaultsInterface::getParentDefaults
WidgetDefaults::__construct public function WidgetDefaults constructor.