コード例 #1
0
 /**
  * {@inheritdoc}
  */
 public function getContextValue()
 {
     if (!isset($this->contextData)) {
         $definition = $this->getContextDefinition();
         if ($definition->isRequired()) {
             $type = $definition->getDataType();
             throw new ContextException(String::format("The @type context is required and not present.", array('@type' => $type)));
         }
         return NULL;
     }
     return $this->contextData->getValue();
 }
コード例 #2
0
ファイル: Context.php プロジェクト: anatalsceo/en-classe
 /**
  * {@inheritdoc}
  */
 public function getContextValue()
 {
     if (!isset($this->contextData)) {
         $definition = $this->getContextDefinition();
         if ($definition->isRequired()) {
             $type = $definition->getDataType();
             throw new ContextException(String::format("The @type context is required and not present.", array('@type' => $type)));
         }
         return NULL;
     }
     // Special case entities.
     // @todo: Remove once entities do not implemented TypedDataInterface.
     if ($this->contextData instanceof ContentEntityInterface) {
         return $this->contextData;
     }
     return $this->contextData->getValue();
 }
コード例 #3
0
 /**
  * Recursive helper for getting all contained strings,
  * i.e. properties of type string.
  */
 public function getContainedStrings(TypedDataInterface $wrapper, $depth, array &$strings)
 {
     if ($wrapper instanceof StringInterface) {
         $strings[] = $wrapper->getValue();
     }
     // Recurse until a certain depth is reached if possible.
     if ($depth < 7) {
         if ($wrapper instanceof \Drupal\Core\TypedData\ListInterface) {
             foreach ($wrapper as $item) {
                 $this->getContainedStrings($item, $depth + 1, $strings);
             }
         } elseif ($wrapper instanceof \Drupal\Core\TypedData\ComplexDataInterface) {
             foreach ($wrapper as $property) {
                 $this->getContainedStrings($property, $depth + 1, $strings);
             }
         }
     }
 }
コード例 #4
0
ファイル: TypedDataManager.php プロジェクト: aWEBoLabs/taxi
 /**
  * {@inheritdoc}
  */
 public function getCanonicalRepresentation(TypedDataInterface $data)
 {
     $data_definition = $data->getDataDefinition();
     // In case a list is passed, respect the 'wrapped' key of its data type.
     if ($data_definition instanceof ListDataDefinitionInterface) {
         $data_definition = $data_definition->getItemDefinition();
     }
     // Get the plugin definition of the used data type.
     $type_definition = $this->getDefinition($data_definition->getDataType());
     if (!empty($type_definition['unwrap_for_canonical_representation'])) {
         return $data->getValue();
     }
     return $data;
 }
コード例 #5
0
ファイル: Utility.php プロジェクト: curveagency/intranet
 /**
  * Extracts field values from a typed data object.
  *
  * @param \Drupal\Core\TypedData\TypedDataInterface $data
  *   The typed data object.
  *
  * @return array
  *   An array of values.
  */
 public static function extractFieldValues(TypedDataInterface $data)
 {
     if ($data->getDataDefinition()->isList()) {
         $values = array();
         foreach ($data as $piece) {
             $values[] = self::extractFieldValues($piece);
         }
         return $values ? call_user_func_array('array_merge', $values) : array();
     }
     $value = $data->getValue();
     $definition = $data->getDataDefinition();
     if ($definition instanceof ComplexDataDefinitionInterface) {
         $property = $definition->getMainPropertyName();
         if (isset($value[$property])) {
             return array($value[$property]);
         }
     } elseif (is_array($value)) {
         return array_values($value);
     }
     return array($value);
 }
コード例 #6
0
 /**
  * Gets translatable configuration data for a typed configuration element.
  *
  * @param \Drupal\Core\TypedData\TypedDataInterface $element
  *   Typed configuration element.
  *
  * @return array|\Drupal\Core\StringTranslation\TranslatableMarkup
  *   A nested array matching the exact structure under $element with only the
  *   elements that are translatable wrapped into a TranslatableMarkup. If the
  *   provided $element is not traversable, the return value is a single
  *   TranslatableMarkup.
  */
 protected function getTranslatableData(TypedDataInterface $element)
 {
     $translatable = array();
     if ($element instanceof TraversableTypedDataInterface) {
         foreach ($element as $key => $property) {
             $value = $this->getTranslatableData($property);
             if (!empty($value)) {
                 $translatable[$key] = $value;
             }
         }
     } else {
         // Something is only translatable by Locale if there is a string in the
         // first place.
         $value = $element->getValue();
         $definition = $element->getDataDefinition();
         if (!empty($definition['translatable']) && $value !== '' && $value !== NULL) {
             $options = array();
             if (isset($definition['translation context'])) {
                 $options['context'] = $definition['translation context'];
             }
             return new TranslatableMarkup($value, array(), $options);
         }
     }
     return $translatable;
 }
コード例 #7
0
 /**
  * Translates element's value if it fits our translation criteria.
  *
  * For an element to be translatable by locale module it needs to be of base
  * type 'string' and have 'translatable = TRUE' in the element's definition.
  * Translatable elements may use these additional keys in their data
  * definition:
  * - 'translatable', FALSE to opt out of translation.
  * - 'translation context', to define the string context.
  *
  * @param \Drupal\Core\TypedData\TypedDataInterface $element
  *   Configuration element.
  * @param array $options
  *   Array with translation options that must contain the following keys:
  *   - 'source', Source language code.
  *   - 'target', Target language code.
  *
  * @return bool
  *   Whether the element fits the translation criteria.
  */
 protected function translateElement(TypedDataInterface $element, array $options)
 {
     if ($this->canTranslate($options['source'], $options['target'])) {
         $definition = $element->getDataDefinition();
         $value = $element->getValue();
         if ($value && !empty($definition['translatable'])) {
             $context = isset($definition['translation context']) ? $definition['translation context'] : '';
             if ($translation = $this->localeConfig->translateString($this->name, $options['target'], $value, $context)) {
                 $element->setValue($translation);
                 return TRUE;
             }
         }
     }
     // The element does not have a translation.
     return FALSE;
 }
コード例 #8
0
 /**
  * Gets translatable configuration data for a typed configuration element.
  *
  * @param \Drupal\Core\TypedData\TypedDataInterface $element
  *   Typed configuration element.
  *
  * @return array|\Drupal\Core\StringTranslation\TranslationWrapper
  *   A nested array matching the exact structure under $element with only the
  *   elements that are translatable wrapped into a TranslationWrapper. If the
  *   provided $element is not traversable, the return value is a single
  *   TranslationWrapper.
  */
 protected function getTranslatableData(TypedDataInterface $element)
 {
     $translatable = array();
     if ($element instanceof TraversableTypedDataInterface) {
         foreach ($element as $key => $property) {
             $value = $this->getTranslatableData($property);
             if (!empty($value)) {
                 $translatable[$key] = $value;
             }
         }
     } else {
         $definition = $element->getDataDefinition();
         if (!empty($definition['translatable'])) {
             $options = array();
             if (isset($definition['translation context'])) {
                 $options['context'] = $definition['translation context'];
             }
             return new TranslationWrapper($element->getValue(), array(), $options);
         }
     }
     return $translatable;
 }
コード例 #9
0
 /**
  * Extracts value and original type from a single piece of data.
  *
  * @param \Drupal\Core\TypedData\TypedDataInterface $data
  *   The piece of data from which to extract information.
  * @param \Drupal\search_api\Item\FieldInterface $field
  *   The field into which to put the extracted data.
  */
 public static function extractField(TypedDataInterface $data, FieldInterface $field)
 {
     if ($data->getDataDefinition()->isList()) {
         foreach ($data as $piece) {
             self::extractField($piece, $field);
         }
         return;
     }
     $value = $data->getValue();
     $definition = $data->getDataDefinition();
     if ($definition instanceof ComplexDataDefinitionInterface) {
         $property = $definition->getMainPropertyName();
         if (isset($value[$property])) {
             $value = $value[$property];
         }
     } elseif (is_array($value)) {
         $value = reset($value);
     }
     // If the data type of the field is a custom one, then the value can be
     // altered by the data type plugin.
     $data_type_manager = \Drupal::service('plugin.manager.search_api.data_type');
     if ($data_type_manager->hasDefinition($field->getType())) {
         $value = $data_type_manager->createInstance($field->getType())->getValue($value);
     }
     $field->addValue($value);
     $field->setOriginalType($definition->getDataType());
 }
コード例 #10
0
ファイル: DataFetcher.php プロジェクト: Progressable/openway8
 /**
  * Adds the bubbleable metadata of the given data.
  *
  * @param \Drupal\Core\TypedData\TypedDataInterface $data
  *   The data of which to add the metadata.
  * @param \Drupal\Core\Render\BubbleableMetadata $bubbleable_metadata
  *   The bubbleable metadata to which to add the data.
  */
 protected function addBubbleableMetadata(TypedDataInterface $data, BubbleableMetadata $bubbleable_metadata)
 {
     if ($data instanceof PrimitiveInterface) {
         // Primitives do not have any metadata attached.
         return;
     }
     $value = $data->getValue();
     if ($value instanceof CacheableDependencyInterface || $value instanceof AttachmentsInterface) {
         $bubbleable_metadata->addCacheableDependency($value);
     }
 }
コード例 #11
0
 /**
  * Extracts the value of the property from the given container.
  *
  * @param mixed $container The container to extract the property value from.
  *
  * @return mixed The value of the property.
  */
 public function getPropertyValue($container)
 {
     return $this->typedData->getValue();
 }
コード例 #12
0
 /**
  * Formats a value as a string, for readable output.
  *
  * @param \Drupal\Core\TypedData\TypedDataInterface $element
  *   The value element.
  *
  * @return string
  *   The value in string form.
  */
 protected function formatValue(TypedDataInterface $element)
 {
     $value = $element->getValue();
     if (is_scalar($value)) {
         return SafeMarkup::checkPlain($value);
     }
     if (empty($value)) {
         return '<' . $this->t('empty') . '>';
     }
     return '<' . gettype($value) . '>';
 }