getTargetIdentifier example



  /** * {@inheritdoc} */
  public function onChange($property_name$notify = TRUE) {
    // Make sure that the value and the language property stay in sync.     if ($property_name == 'value') {
      $this->writePropertyValue('language', $this->value);
    }
    elseif ($property_name == 'language') {
      $this->writePropertyValue('value', $this->get('language')->getTargetIdentifier());
    }
    parent::onChange($property_name$notify);
  }

  /** * {@inheritdoc} */
  public static function generateSampleValue(FieldDefinitionInterface $field_definition) {
    // Defer to the callback in the item definition as it can be overridden.     $constraint = $field_definition->getItemDefinition()->getConstraint('ComplexData');
    if (isset($constraint['value']['AllowedValues']['callback'])) {
      
      if (is_array($values) && array_key_exists('target_id', $values) && !isset($values['entity'])) {
        $this->onChange('target_id', FALSE);
      }
      elseif (is_array($values) && !array_key_exists('target_id', $values) && isset($values['entity'])) {
        $this->onChange('entity', FALSE);
      }
      elseif (is_array($values) && array_key_exists('target_id', $values) && isset($values['entity'])) {
        // If both properties are passed, verify the passed values match. The         // only exception we allow is when we have a new entity: in this case         // its actual id and target_id will be different, due to the new entity         // marker.         $entity_id = $this->get('entity')->getTargetIdentifier();
        // If the entity has been saved and we're trying to set both the         // target_id and the entity values with a non-null target ID, then the         // value for target_id should match the ID of the entity value. The         // entity ID as returned by $entity->id() might be a string, but the         // provided target_id might be an integer - therefore we have to do a         // non-strict comparison.         if (!$this->entity->isNew() && $values['target_id'] !== NULL && ($entity_id != $values['target_id'])) {
          throw new \InvalidArgumentException('The target id and entity passed to the entity reference item do not match.');
        }
      }
      // Notify the parent if necessary.
$this->parent->onChange($this->name);
    }
  }

  /** * {@inheritdoc} */
  public function getString() {
    if (!method_exists($this, 'getType')) {
      throw new \BadMethodCallException(get_class($this) . '::getType() not implemented');
    }
    return (string) $this->getType() . ':' . $this->getTargetIdentifier();
  }

}
Home | Imprint | This part of the site doesn't use cookies.