You are here

public function SimpleFlowSetupHelper::enableBundle in CMS Content Sync 2.1.x

File

src/Helper/SimpleFlowSetupHelper.php, line 26

Class

SimpleFlowSetupHelper

Namespace

Drupal\cms_content_sync\Helper

Code

public function enableBundle(string $entity_type_name, string $bundle, bool $dependency = false, ?array $filter_by_tags = null) {
  $this->flow->simple_settings['entityTypeSettings'][$entity_type_name]['perBundle'][$bundle] = [
    'mode' => $dependency ? FlowControllerSimple::MODE_DEPENDENT : 'default',
  ];
  if (!empty($filter_by_tags)) {

    /**
     * @var \Drupal\Core\Entity\EntityFieldManagerInterface $entityFieldManager
     */
    $entityFieldManager = \Drupal::service('entity_field.manager');
    $fields = $entityFieldManager
      ->getFieldDefinitions($entity_type_name, $bundle);
    foreach ($fields as $field_name => $field) {
      if ('entity_reference' == $field
        ->getType() && 'taxonomy_term' == $field
        ->getSetting('target_type')) {
        $bundles = $field
          ->getSetting('target_bundles');
        if (is_array($bundles)) {
          $terms = [];
          foreach ($filter_by_tags as $tag) {
            if (in_array($tag
              ->bundle(), $bundles)) {
              $terms[] = [
                'namespaceMachineName' => 'taxonomy_term',
                'machineName' => $tag
                  ->bundle(),
                'remoteUuid' => $tag
                  ->uuid(),
              ];
            }
          }
          if (count($terms)) {
            $this->flow->simple_settings['entityTypeSettings'][$entity_type_name]['perBundle'][$bundle]['filterByReference'][] = [
              'type' => 'includes-reference',
              'fieldMachineName' => $field_name,
              'values' => $terms,
            ];
          }
        }
      }
    }
  }
}