You are here

public function Flagging::onChange in Flag 8.4

Reacts to changes to a field.

Note that this is invoked after any changes have been applied.

Parameters

string $field_name: The name of the field which is changed.

Throws

\InvalidArgumentException When trying to assign a value to the language field that matches an existing translation.

\LogicException When trying to change:

  • The language of a translation.
  • The value of the flag identifying the default translation object.

Overrides ContentEntityBase::onChange

File

src/Entity/Flagging.php, line 160

Class

Flagging
Provides the flagging content entity.

Namespace

Drupal\flag\Entity

Code

public function onChange($name) {
  if ($name == 'entity_id' && $this
    ->get('flagged_entity')
    ->isEmpty()) {
    $this->flagged_entity->target_id = $this->entity_id->value;
  }
  if (in_array($name, [
    'flagged_entity',
    'entity_id',
  ]) && $this->flagged_entity->target_id != $this->entity_id->value) {
    throw new \LogicException("A flagging can't be moved to another entity.");
  }
  parent::onChange($name);
}