/** * {@inheritdoc} */ public function getStates($all = FALSE, $reset = FALSE) { $wid = $this->id(); if ($reset) { $this->states = $wid ? WorkflowState::loadMultiple([], $wid, $reset) : array(); } elseif ($this->states === NULL) { $this->states = $wid ? WorkflowState::loadMultiple([], $wid, $reset) : array(); } elseif ($this->states === array()) { $this->states = $wid ? WorkflowState::loadMultiple([], $wid, $reset) : array(); } // Do not unset, but add to array - you'll remove global objects otherwise. $states = array(); foreach ($this->states as $state) { $id = $state->id(); if ($all === TRUE) { $states[$id] = $state; } elseif ($all === FALSE && ($state->isActive() && !$state->isCreationState())) { $states[$id] = $state; } elseif ($all == 'CREATION' && ($state->isActive() || $state->isCreationState())) { $states[$id] = $state; } else { // Do not add state. } } return $states; }
/** * Implements hook_field_settings_form() -> ConfigFieldItemInterface::settingsForm(). */ public function storageSettingsForm(array &$form, FormStateInterface $form_state, $has_data) { $element = array(); // Create list of all Workflow types. Include an initial empty value. // Validate each workflow, and generate a message if not complete. $workflows = workflow_get_workflow_names(FALSE); // @todo D8: add this to WorkflowFieldConstraintValidator. // Set message, if no 'validated' workflows exist. if (count($workflows) == 1) { drupal_set_message(t('You must create at least one workflow before content can be assigned to a workflow.'), 'warning'); } // Validate via annotation WorkflowFieldConstraint. Show a message for each error. $violation_list = $this->validate(); foreach ($violation_list->getIterator() as $violation) { switch ($violation->getPropertyPath()) { case 'fieldnameOnComment': // A 'comment' field name MUST be equal to content field name. // @todo: Still not waterproof. You could have a field on a non-relevant entity_type. drupal_set_message($violation->getMessage(), 'error'); $workflows = array(); break; default: break; } } // Set the required workflow_type on 'comment' fields. // N.B. the following must BELOW the (count($workflows) == 1) snippet. $field_storage = $this->getFieldDefinition()->getFieldStorageDefinition(); if (!$this->getSetting('workflow_type') && $field_storage->getTargetEntityTypeId() == 'comment') { $field_name = $field_storage->get('field_name'); $workflows = array(); foreach (_workflow_info_fields($entity = NULL, $entity_type = '', $entity_bundle = '', $field_name) as $key => $info) { if ($info->getName() == $field_name && $info->getTargetEntityTypeId() !== 'comment') { $wid = $info->getSetting('workflow_type'); $workflow = Workflow::load($wid); $workflows[$wid] = $workflow->label(); } } } // Let the user choose between the available workflow types. $wid = $this->getSetting('workflow_type'); $url = \Drupal\Core\Url::fromRoute('entity.workflow_type.collection'); $element['workflow_type'] = array('#type' => 'select', '#title' => t('Workflow type'), '#options' => $workflows, '#default_value' => $wid, '#required' => TRUE, '#disabled' => $has_data, '#description' => t('Choose the Workflow type. Maintain workflows <a href=":url">here</a>.', array(':url' => $url->toString()))); // Get a string representation to show all options. /* * Overwrite ListItemBase::storageSettingsForm(). */ $allowed_values = WorkflowState::loadMultiple([], $wid); $allowed_values_function = $this->getSetting('allowed_values_function'); $element['allowed_values'] = array('#type' => 'textarea', '#title' => t('Allowed values for the selected Workflow type'), '#default_value' => $wid ? $this->allowedValuesString($allowed_values) : [], '#rows' => count($allowed_values), '#access' => $wid ? TRUE : FALSE, '#disabled' => TRUE, '#element_validate' => array(array(get_class($this), 'validateAllowedValues')), '#field_has_data' => $has_data, '#field_name' => $this->getFieldDefinition()->getName(), '#entity_type' => $this->getEntity()->getEntityTypeId(), '#allowed_values' => $allowed_values); $element['allowed_values']['#description'] = $this->allowedValuesDescription(); return $element; }