You are here

class TypedDataRelationshipDeriver in Chaos Tool Suite (ctools) 8.3

Hierarchy

Expanded class hierarchy of TypedDataRelationshipDeriver

File

src/Plugin/Deriver/TypedDataRelationshipDeriver.php, line 12

Namespace

Drupal\ctools\Plugin\Deriver
View source
class TypedDataRelationshipDeriver extends TypedDataPropertyDeriverBase implements ContainerDeriverInterface {

  /**
   * {@inheritdoc}
   */
  protected function generateDerivativeDefinition($base_plugin_definition, $data_type_id, $data_type_definition, DataDefinitionInterface $base_definition, $property_name, DataDefinitionInterface $property_definition) {
    $bundle_info = $base_definition
      ->getConstraint('Bundle');

    // Identify base definitions that appear on bundle-able entities.
    if ($bundle_info && array_filter($bundle_info) && $base_definition
      ->getConstraint('EntityType')) {
      $base_data_type = 'entity:' . $base_definition
        ->getConstraint('EntityType');
    }
    else {
      $base_data_type = $data_type_id;
    }

    // If we've not processed this thing before.
    if (!isset($this->derivatives[$base_data_type . ':' . $property_name])) {
      $derivative = $base_plugin_definition;
      $derivative['label'] = $this
        ->t($this->label, [
        '@property' => $property_definition
          ->getLabel(),
        '@base' => $data_type_definition['label'],
      ]);
      $derivative['data_type'] = $property_definition
        ->getFieldStorageDefinition()
        ->getPropertyDefinition($property_definition
        ->getFieldStorageDefinition()
        ->getMainPropertyName())
        ->getDataType();
      $derivative['property_name'] = $property_name;
      if (strpos($base_data_type, 'entity:') === 0) {
        $context_definition = new EntityContextDefinition($base_data_type, $this->typedDataManager
          ->createDataDefinition($base_data_type));
      }
      else {
        $context_definition = new ContextDefinition($base_data_type, $this->typedDataManager
          ->createDataDefinition($base_data_type));
      }

      // Add the constraints of the base definition to the context definition.
      if ($base_definition
        ->getConstraint('Bundle')) {
        $context_definition
          ->addConstraint('Bundle', $base_definition
          ->getConstraint('Bundle'));
      }
      $derivative['context_definitions'] = [
        'base' => $context_definition,
      ];
      $derivative['property_name'] = $property_name;
      $this->derivatives[$base_data_type . ':' . $property_name] = $derivative;
    }
    elseif ($property_definition instanceof FieldConfigInterface) {

      // We should only end up in here on entity bundles.
      $derivative = $this->derivatives[$base_data_type . ':' . $property_name];

      // Update label.

      /** @var \Drupal\Core\StringTranslation\TranslatableMarkup $label */
      $label = $derivative['label'];
      list(, , $argument_name) = explode(':', $data_type_id);
      $arguments = $label
        ->getArguments();
      $arguments['@' . $argument_name] = $data_type_definition['label'];
      $string_args = $arguments;
      array_shift($string_args);
      $last = array_slice($string_args, -1);

      // The slice doesn't remove, so do that now.
      array_pop($string_args);
      $string = count($string_args) >= 2 ? '@property from ' . implode(', ', array_keys($string_args)) . ' and ' . array_keys($last)[0] : '@property from @base and ' . array_keys($last)[0];
      $this->derivatives[$base_data_type . ':' . $property_name]['label'] = $this
        ->t($string, $arguments);
      if ($base_definition
        ->getConstraint('Bundle')) {

        // Add bundle constraints.
        $context_definition = $derivative['context_definitions']['base'];
        $bundles = $context_definition
          ->getConstraint('Bundle') ?: [];
        $bundles = array_merge($bundles, $base_definition
          ->getConstraint('Bundle'));
        $context_definition
          ->addConstraint('Bundle', $bundles);
      }
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DeriverBase::$derivatives protected property List of derivative definitions. 1
DeriverBase::getDerivativeDefinition public function Gets the definition of a derivative plugin. Overrides DeriverInterface::getDerivativeDefinition
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
TypedDataPropertyDeriverBase::$label protected property The label string for use with derivative labels. 2
TypedDataPropertyDeriverBase::$typedDataManager protected property
TypedDataPropertyDeriverBase::create public static function Creates a new class instance. Overrides ContainerDeriverInterface::create
TypedDataPropertyDeriverBase::getDataType protected function
TypedDataPropertyDeriverBase::getDerivativeDefinitions public function Gets the definition of all derivatives of a base plugin. Overrides DeriverBase::getDerivativeDefinitions 1
TypedDataPropertyDeriverBase::__construct public function TypedDataPropertyDeriverBase constructor.
TypedDataRelationshipDeriver::generateDerivativeDefinition protected function Generates and maintains a derivative definition. Overrides TypedDataPropertyDeriverBase::generateDerivativeDefinition 2