Skip to content
Snippets Groups Projects
Select Git revision
  • e93b685205fa5aa261953b5f14db1a7d2c0ff0ed
  • 11.x default protected
  • 11.2.x protected
  • 10.5.x protected
  • 10.6.x protected
  • 11.1.x protected
  • 10.4.x protected
  • 11.0.x protected
  • 10.3.x protected
  • 7.x protected
  • 10.2.x protected
  • 10.1.x protected
  • 9.5.x protected
  • 10.0.x protected
  • 9.4.x protected
  • 9.3.x protected
  • 9.2.x protected
  • 9.1.x protected
  • 8.9.x protected
  • 9.0.x protected
  • 8.8.x protected
  • 10.5.1 protected
  • 11.2.2 protected
  • 11.2.1 protected
  • 11.2.0 protected
  • 10.5.0 protected
  • 11.2.0-rc2 protected
  • 10.5.0-rc1 protected
  • 11.2.0-rc1 protected
  • 10.4.8 protected
  • 11.1.8 protected
  • 10.5.0-beta1 protected
  • 11.2.0-beta1 protected
  • 11.2.0-alpha1 protected
  • 10.4.7 protected
  • 11.1.7 protected
  • 10.4.6 protected
  • 11.1.6 protected
  • 10.3.14 protected
  • 10.4.5 protected
  • 11.0.13 protected
41 results

edit.module

Blame
  • Alex Pott's avatar
    Issue #2202671 by Mark Carver, tim.plunkett: Rename CSS_SKIN constant to CSS_THEME.
    Alex Pott authored
    e93b6852
    History
    Code owners
    Assign users and groups as approvers for specific file changes. Learn more.
    edit.module 4.67 KiB
    <?php
    
    /**
     * @file
     * Provides in-place content editing functionality for fields.
     *
     * The Edit module makes content editable in-place. Rather than having to visit
     * a separate page to edit content, it may be edited in-place.
     *
     * Technically, this module adds classes and data- attributes to fields and
     * entities, enabling them for in-place editing.
     */
    
    use Drupal\Core\Entity\EntityInterface;
    use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
    
    /**
     * Implements hook_permission().
     */
    function edit_permission() {
      return array(
        'access in-place editing' => array(
          'title' => t('Access in-place editing'),
        ),
      );
    }
    
    /**
     * Implements hook_page_build().
     *
     * Adds the edit library to the page for any user who has the 'access in-place
     * editing' permission.
     */
    function edit_page_build(&$page) {
      if (!\Drupal::currentUser()->hasPermission('access in-place editing')) {
        return;
      }
    
      // In-place editing is only supported on the front-end.
      $path = \Drupal::request()->attributes->get('_system_path');
      if (path_is_admin($path)) {
        return;
      }
    
      $page['#attached']['library'][] = array('edit', 'edit');
    }
    
    /**
     * Implements hook_library_alter().
     *
     * Includes additional stylesheets defined by the admin theme to allow it to
     * customize the Edit toolbar appearance.
     *
     * An admin theme can specify CSS files to make the front-end administration
     * experience of in-place editing match the administration experience in the
     * back-end.
     *
     * The CSS files can be specified via the "edit_stylesheets" property in the
     * .info.yml file:
     * @code
     * edit_stylesheets:
     *   - css/edit.css
     * @endcode
     *
     * The library needs to be dynamically enhanced, because an admin theme normally
     * does not participate in the front-end.
     *
     * @param string $theme
     *   (optional) Internal use only. A base theme name for which to retrieve the
     *   'edit_stylesheets' property.
     *
     * @todo Remove this in favor of the 'stylesheets-additional' property proposed
     *   in https://drupal.org/node/1209958
     */
    function edit_library_alter(array &$library, $extension, $name, $theme = NULL) {
      if ($extension == 'edit' && $name == 'edit') {
        // Retrieve the admin theme.
        if (!isset($theme)) {
          $theme = Drupal::config('system.theme')->get('admin');
        }
        if ($theme && $theme_path = drupal_get_path('theme', $theme)) {
          $info = system_get_info('theme', $theme);
          // Recurse to process base theme(s) first.
          if (isset($info['base theme'])) {
            edit_library_alter($library, $extension, $name, $info['base theme']);
          }
          if (isset($info['edit_stylesheets']) && is_array($info['edit_stylesheets'])) {
            foreach ($info['edit_stylesheets'] as $path) {
              $library['css'][$theme_path . '/' . $path] = array(
                'group' => CSS_AGGREGATE_THEME,
                'weight' => CSS_THEME,
              );
            }
          }
        }
      }
    }
    
    /**
     * Implements hook_field_formatter_info_alter().
     *
     * Edit extends the @FieldFormatter annotation with the following keys:
     * - edit: currently only contains one subkey 'editor' which indicates which
     *   in-place editor should be used. Possible values are 'form', 'plain_text',
     *   'disabled' or another in-place editor other than the ones Edit module
     *   provides.
     */
    function edit_field_formatter_info_alter(&$info) {
      foreach ($info as $key => $settings) {
        // Set in-place editor to 'form' if none is supplied.
        if (empty($settings['edit'])) {
          $info[$key]['edit'] = array('editor' => 'form');
        }
      }
    }
    
    /**
     * Implements hook_preprocess_HOOK() for field templates.
     */
    function edit_preprocess_field(&$variables) {
      $element = $variables['element'];
      /** @var $entity \Drupal\Core\Entity\EntityInterface */
      $entity = $element['#object'];
    
      // Edit module only supports view modes, not dynamically defined "display
      // options" (which field_view_field() always names the "_custom" view mode).
      // @see field_view_field()
      // @see https://drupal.org/node/2120335
      if ($element['#view_mode'] === '_custom') {
        return;
      }
    
      // Fields that are not part of the entity (i.e. dynamically injected "pseudo
      // fields") and computed fields are not editable.
      $definition = $entity->getFieldDefinition($element['#field_name']);
      if ($definition && !$definition->isComputed()) {
        $variables['attributes']['data-edit-field-id'] = $entity->getEntityTypeId() . '/' . $entity->id() . '/' . $element['#field_name'] . '/' . $element['#language'] . '/' . $element['#view_mode'];
      }
    }
    
    /**
     * Implements hook_entity_view_alter().
     */
    function edit_entity_view_alter(&$build, EntityInterface $entity, EntityViewDisplayInterface $display) {
      $build['#attributes']['data-edit-entity-id'] = $entity->getEntityTypeId() . '/' . $entity->id();
    }