Skip to content
Snippets Groups Projects
Commit 24409f3a authored by Owen Bush's avatar Owen Bush
Browse files

Refactor the inheritance plugin to make it easier to override certain inheritance strategy/methods

parent 2157e78f
No related branches found
No related tags found
No related merge requests found
......@@ -104,56 +104,94 @@ abstract class FieldInheritancePluginBase extends PluginBase implements FieldInh
public function computeValue() {
$this->validateArguments();
$method = $this->getMethod();
$field = $this->getSourceField();
$instance = $this->entity;
$series = $instance->getEventSeries();
$value = '';
switch ($method) {
case 'inherit':
$value = $series->{$field}->value ?? '';
$value = $this->inheritData();
break;
case 'prepend':
$entity_field = $this->getEntityField();
$fields = [];
if (!empty($instance->{$entity_field}->value)) {
$fields[] = $instance->{$entity_field}->value;
}
if (!empty($series->{$field}->value)) {
$fields[] = $series->{$field}->value;
}
$value = implode($this::SEPARATOR, $fields);
$value = $this->prependData();
break;
case 'append':
$entity_field = $this->getEntityField();
$fields = [];
if (!empty($series->{$field}->value)) {
$fields[] = $series->{$field}->value;
}
if (!empty($instance->{$entity_field}->value)) {
$fields[] = $instance->{$entity_field}->value;
}
$value = implode($this::SEPARATOR, $fields);
$value = $this->appendData();
break;
case 'fallback':
$entity_field = $this->getEntityField();
$value = $this->fallbackData();
break;
}
return $value;
}
$value = '';
/**
* Retrieve inherited data.
*
* @return string
* The inherited data.
*/
protected function inheritData() {
$series = $this->entity->getEventSeries();
return $series->{$this->getSourceField()}->value ?? '';
}
if (!empty($instance->{$entity_field}->value)) {
$value = $instance->{$entity_field}->value;
}
elseif (!empty($series->{$field}->value)) {
$value = $series->{$field}->value;
}
/**
* Retrieve prepended data.
*
* @return string
* The prepended data.
*/
protected function prependData() {
$series = $this->entity->getEventSeries();
$instance = $this->entity;
break;
$fields = [];
if (!empty($instance->{$this->getEntityField()}->value)) {
$fields[] = $instance->{$this->getEntityField()}->value;
}
if (!empty($series->{$this->getSourceField()}->value)) {
$fields[] = $series->{$this->getSourceField()}->value;
}
return implode($this::SEPARATOR, $fields);
}
/**
* Retrieve appended data.
*
* @return string
* The appended data.
*/
protected function appendData() {
$series = $this->entity->getEventSeries();
$instance = $this->entity;
$fields = [];
if (!empty($series->{$this->getSourceField()}->value)) {
$fields[] = $series->{$this->getSourceField()}->value;
}
if (!empty($instance->{$this->getEntityField()}->value)) {
$fields[] = $instance->{$this->getEntityField()}->value;
}
return implode($this::SEPARATOR, $fields);
}
/**
* Retrieve fallback data.
*
* @return string
* The fallback data.
*/
protected function fallbackData() {
$series = $this->entity->getEventSeries();
$instance = $this->entity;
if (!empty($instance->{$this->getEntityField()}->value)) {
$value = $instance->{$this->getEntityField()}->value;
}
elseif (!empty($series->{$this->getSourceField()}->value)) {
$value = $series->{$this->getSourceField()}->value;
}
return $value;
}
......
......@@ -2,6 +2,8 @@
namespace Drupal\recurring_events\Plugin\FieldInheritance;
use Drupal\recurring_events\FieldInheritancePluginInterface;
/**
* String Inheritance plugin.
*
......@@ -13,7 +15,7 @@ namespace Drupal\recurring_events\Plugin\FieldInheritance;
* }
* )
*/
class StringFieldInheritancePlugin extends FieldInheritancePluginBase {
class StringFieldInheritancePlugin extends FieldInheritancePluginBase implements FieldInheritancePluginInterface {
/**
* Concatenation separator.
......
......@@ -2,6 +2,8 @@
namespace Drupal\recurring_events\Plugin\FieldInheritance;
use Drupal\recurring_events\FieldInheritancePluginInterface;
/**
* Text Long Inheritance plugin.
*
......@@ -13,7 +15,7 @@ namespace Drupal\recurring_events\Plugin\FieldInheritance;
* }
* )
*/
class TextLongFieldInheritancePlugin extends FieldInheritancePluginBase {
class TextLongFieldInheritancePlugin extends FieldInheritancePluginBase implements FieldInheritancePluginInterface {
/**
* Concatenation separator.
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment