/**
  * Implements EditMetadataGeneratorInterface::generate().
  */
 public function generate($entity_type, $entity, array $instance, $langcode, $view_mode)
 {
     $field_name = $instance['field_name'];
     // Early-return if user does not have access.
     $access = $this->accessChecker->accessEditEntityField($entity_type, $entity, $field_name);
     if (!$access) {
         return array('access' => FALSE);
     }
     // Early-return if no editor is available.
     if (!_edit_is_extra_field($entity_type, $field_name)) {
         $display = field_get_display($instance, $view_mode, $entity);
         $formatter_type = field_info_formatter_types($display['type']);
         $items = field_get_items($entity_type, $entity, $field_name, $langcode);
         $items = $items === FALSE ? array() : $items;
         $editor_id = $this->editorSelector->getEditor($formatter_type, $instance, $items);
     } else {
         // @see hook_edit_extra_fields_info()
         $extra = edit_extra_field_info($entity_type, $field_name);
         if (isset($extra['view mode dependent editor'][$view_mode])) {
             $editor_id = $extra['view mode dependent editor'][$view_mode];
         } else {
             $editor_id = $extra['default editor'];
         }
     }
     if (!isset($editor_id)) {
         return array('access' => FALSE);
     }
     // Gather metadata, allow the editor to add additional metadata of its own.
     if (!_edit_is_extra_field($entity_type, $field_name)) {
         $label = $instance['label'];
     } else {
         $label = edit_extra_field_info($entity_type, $field_name, 'label');
     }
     list($id, $vid, $bundle) = entity_extract_ids($entity_type, $entity);
     $metadata = array('label' => check_plain($label), 'access' => TRUE, 'editor' => $editor_id, 'aria' => t('Entity @type @id, field @field', array('@type' => $entity_type, '@id' => $id, '@field' => $label)));
     if (!_edit_is_extra_field($entity_type, $field_name)) {
         $editor = edit_editor_get($editor_id);
         if (!empty($editor['metadata callback'])) {
             if ($editor['file']) {
                 require_once $editor['file path'] . '/' . $editor['file'];
             }
             if (function_exists($editor['metadata callback'])) {
                 $custom_metadata = $editor['metadata callback']($instance, $items);
                 if (count($custom_metadata)) {
                     $metadata['custom'] = $custom_metadata;
                 }
             }
         }
         // Allow the metadata to be altered.
         $context = array('entity_type' => $entity_type, 'entity' => $entity, 'field' => $instance, 'items' => $items);
         drupal_alter('edit_editor_metadata', $metadata, $editor_id, $context);
     }
     return $metadata;
 }
 /**
  * Implements EditEditorSelectorInterface::getEditorAttachments().
  */
 public function getEditorAttachments(array $editor_ids, array $metadata)
 {
     $attachments = array();
     // Editor plugins' attachments.
     foreach (array_unique($editor_ids) as $editor_id) {
         $editor = edit_editor_get($editor_id);
         if (!empty($editor['attachments callback'])) {
             if ($editor['file']) {
                 require_once $editor['file path'] . '/' . $editor['file'];
             }
             if (function_exists($editor['attachments callback'])) {
                 $attachments[$editor_id] = $editor['attachments callback']($metadata);
                 // Allows contrib to declare additional dependencies for the editor.
                 drupal_alter('edit_editor_attachments', $attachments[$editor_id], $editor_id, $metadata);
             }
         }
     }
     // JavaScript settings for Edit.
     foreach (array_unique($editor_ids) as $editor_id) {
         $editor = edit_editor_get($editor_id);
         $attachments[] = array('js' => array(array('type' => 'setting', 'data' => array('edit' => array('editors' => array($editor_id => array('widget' => $editor['widget'])))))));
     }
     return drupal_array_merge_deep_array($attachments);
 }