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 ...@@ -104,56 +104,94 @@ abstract class FieldInheritancePluginBase extends PluginBase implements FieldInh
public function computeValue() { public function computeValue() {
$this->validateArguments(); $this->validateArguments();
$method = $this->getMethod(); $method = $this->getMethod();
$field = $this->getSourceField();
$instance = $this->entity;
$series = $instance->getEventSeries();
$value = ''; $value = '';
switch ($method) { switch ($method) {
case 'inherit': case 'inherit':
$value = $series->{$field}->value ?? ''; $value = $this->inheritData();
break; break;
case 'prepend': case 'prepend':
$entity_field = $this->getEntityField(); $value = $this->prependData();
$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);
break; break;
case 'append': case 'append':
$entity_field = $this->getEntityField(); $value = $this->appendData();
$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);
break; break;
case 'fallback': 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; * Retrieve prepended data.
} *
elseif (!empty($series->{$field}->value)) { * @return string
$value = $series->{$field}->value; * 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; return $value;
} }
......
...@@ -2,6 +2,8 @@ ...@@ -2,6 +2,8 @@
namespace Drupal\recurring_events\Plugin\FieldInheritance; namespace Drupal\recurring_events\Plugin\FieldInheritance;
use Drupal\recurring_events\FieldInheritancePluginInterface;
/** /**
* String Inheritance plugin. * String Inheritance plugin.
* *
...@@ -13,7 +15,7 @@ namespace Drupal\recurring_events\Plugin\FieldInheritance; ...@@ -13,7 +15,7 @@ namespace Drupal\recurring_events\Plugin\FieldInheritance;
* } * }
* ) * )
*/ */
class StringFieldInheritancePlugin extends FieldInheritancePluginBase { class StringFieldInheritancePlugin extends FieldInheritancePluginBase implements FieldInheritancePluginInterface {
/** /**
* Concatenation separator. * Concatenation separator.
......
...@@ -2,6 +2,8 @@ ...@@ -2,6 +2,8 @@
namespace Drupal\recurring_events\Plugin\FieldInheritance; namespace Drupal\recurring_events\Plugin\FieldInheritance;
use Drupal\recurring_events\FieldInheritancePluginInterface;
/** /**
* Text Long Inheritance plugin. * Text Long Inheritance plugin.
* *
...@@ -13,7 +15,7 @@ namespace Drupal\recurring_events\Plugin\FieldInheritance; ...@@ -13,7 +15,7 @@ namespace Drupal\recurring_events\Plugin\FieldInheritance;
* } * }
* ) * )
*/ */
class TextLongFieldInheritancePlugin extends FieldInheritancePluginBase { class TextLongFieldInheritancePlugin extends FieldInheritancePluginBase implements FieldInheritancePluginInterface {
/** /**
* Concatenation separator. * 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